src/HOL/Library/Word.thy
author Fabian Huch <huch@in.tum.de>
Fri, 24 Nov 2023 17:24:35 +0100
changeset 79040 7bb8dba028ce
parent 79031 4596a14d9a95
child 79072 a91050cd5c93
permissions -rw-r--r--
tuned;
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"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    10
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    11
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    12
subsection \<open>Preliminaries\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    13
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    14
lemma signed_take_bit_decr_length_iff:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    15
  \<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
    16
    \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    17
  by (cases \<open>LENGTH('a)\<close>)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    18
    (simp_all add: signed_take_bit_eq_iff_take_bit_eq)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    19
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    20
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    21
subsection \<open>Fundamentals\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    22
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    23
subsubsection \<open>Type definition\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    24
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
    25
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
    26
  morphisms rep Word by (auto intro!: equivpI reflpI sympI transpI)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    27
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    28
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
    29
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
    30
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    31
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    32
subsubsection \<open>Basic arithmetic\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    33
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    34
instantiation word :: (len) comm_ring_1
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    35
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    36
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    37
lift_definition zero_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    38
  is 0 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    39
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    40
lift_definition one_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    41
  is 1 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    42
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    43
lift_definition plus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    44
  is \<open>(+)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    45
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    46
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    47
lift_definition minus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    48
  is \<open>(-)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    49
  by (auto simp add: take_bit_eq_mod intro: mod_diff_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    50
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    51
lift_definition uminus_word :: \<open>'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    52
  is uminus
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    53
  by (auto simp add: take_bit_eq_mod intro: mod_minus_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    54
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    55
lift_definition times_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    56
  is \<open>(*)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    57
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    58
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    59
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    60
  by (standard; transfer) (simp_all add: algebra_simps)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    61
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    62
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    63
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    64
context
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    65
  includes lifting_syntax
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    66
  notes
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    67
    power_transfer [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    68
    transfer_rule_of_bool [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    69
    transfer_rule_numeral [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    70
    transfer_rule_of_nat [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    71
    transfer_rule_of_int [transfer_rule]
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    72
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    73
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    74
lemma power_transfer_word [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    75
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    76
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    77
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    78
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    79
  \<open>((=) ===> pcr_word) of_bool of_bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    80
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    81
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    82
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    83
  \<open>((=) ===> pcr_word) numeral numeral\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    84
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    85
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    86
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    87
  \<open>((=) ===> pcr_word) int of_nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    88
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    89
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    90
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    91
  \<open>((=) ===> pcr_word) (\<lambda>k. k) of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    92
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    93
  have \<open>((=) ===> pcr_word) of_int of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    94
    by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    95
  then show ?thesis by (simp add: id_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    96
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    97
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    98
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    99
  \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   100
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   101
  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
   102
    for k :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   103
  proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   104
    assume ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   105
    then show ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   106
      by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   107
  next
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   108
    assume ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   109
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   110
    then have "even (take_bit LENGTH('a) k)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   111
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   112
    then show ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   113
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   114
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   115
  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
   116
    transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   117
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   118
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   119
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   120
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   121
lemma exp_eq_zero_iff [simp]:
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   122
  \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
73535
0f33c7031ec9 new lemmas
haftmann
parents: 72954
diff changeset
   123
  by transfer auto
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   124
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   125
lemma word_exp_length_eq_0 [simp]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   126
  \<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close>
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
   127
  by simp
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   128
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   129
72489
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   130
subsubsection \<open>Basic tool setup\<close>
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   131
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
   132
ML_file \<open>Tools/word_lib.ML\<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
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   135
subsubsection \<open>Basic code generation setup\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   136
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   137
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   138
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   139
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   140
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
   141
  is \<open>take_bit LENGTH('a)\<close> .
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   142
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   143
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   144
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   145
lemma [code abstype]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   146
  \<open>Word.Word (Word.the_int w) = w\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   147
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   148
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   149
lemma Word_eq_word_of_int [code_post, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   150
  \<open>Word.Word = of_int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   151
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   152
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   153
quickcheck_generator word
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   154
  constructors:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   155
    \<open>0 :: 'a::len word\<close>,
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   156
    \<open>numeral :: num \<Rightarrow> 'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   157
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   158
instantiation word :: (len) equal
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   159
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   160
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   161
lift_definition equal_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> bool\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   162
  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
   163
  by simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   164
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   165
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   166
  by (standard; transfer) rule
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   167
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   168
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   169
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   170
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   171
  \<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
   172
  by transfer (simp add: equal)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   173
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   174
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   175
  \<open>Word.the_int 0 = 0\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   176
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   177
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   178
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   179
  \<open>Word.the_int 1 = 1\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   180
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   181
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   182
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   183
  \<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
   184
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   185
  by transfer (simp add: take_bit_add)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   186
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   187
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   188
  \<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
   189
  for w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   190
  by transfer (auto simp add: take_bit_eq_mod zmod_zminus1_eq_if)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   191
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   192
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   193
  \<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
   194
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   195
  by transfer (simp add: take_bit_diff)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   196
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   197
lemma [code]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   198
  \<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
   199
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   200
  by transfer (simp add: take_bit_mult)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   201
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   202
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   203
subsubsection \<open>Basic conversions\<close>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   204
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   205
abbreviation word_of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   206
  where \<open>word_of_nat \<equiv> of_nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   207
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   208
abbreviation word_of_int :: \<open>int \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   209
  where \<open>word_of_int \<equiv> of_int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   210
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   211
lemma word_of_nat_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   212
  \<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
   213
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   214
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   215
lemma word_of_int_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   216
  \<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
   217
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   218
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
   219
lemma word_of_nat_eq_0_iff:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   220
  \<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
   221
  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
   222
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
   223
lemma word_of_int_eq_0_iff:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   224
  \<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
   225
  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
   226
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   227
context semiring_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   228
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   229
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   230
lift_definition unsigned :: \<open>'b::len word \<Rightarrow> 'a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   231
  is \<open>of_nat \<circ> nat \<circ> take_bit LENGTH('b)\<close>
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   232
  by simp
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   233
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   234
lemma unsigned_0 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   235
  \<open>unsigned 0 = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   236
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   237
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   238
lemma unsigned_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   239
  \<open>unsigned 1 = 1\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   240
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   241
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   242
lemma unsigned_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   243
  \<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
   244
  by transfer (simp add: nat_take_bit_eq)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   245
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   246
lemma unsigned_neg_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   247
  \<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
   248
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   249
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   250
end
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
context semiring_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   253
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   254
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
   255
lemma unsigned_of_nat:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   256
  \<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
   257
  by transfer (simp add: nat_eq_iff take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   258
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
   259
lemma unsigned_of_int:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   260
  \<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
   261
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   263
end
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
context semiring_char_0
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   266
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   267
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   268
lemma unsigned_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   269
  \<open>v = w\<close> if \<open>unsigned v = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   270
  using that by transfer (simp add: eq_nat_nat_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   271
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   272
lemma word_eq_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   273
  \<open>v = w \<longleftrightarrow> unsigned v = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   274
  by (auto intro: unsigned_word_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   275
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   276
lemma inj_unsigned [simp]:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   277
  \<open>inj unsigned\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   278
  by (rule injI) (simp add: unsigned_word_eqI)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   279
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   280
lemma unsigned_eq_0_iff:
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   281
  \<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
   282
  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
   283
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   284
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   285
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   286
context ring_1
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   287
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   288
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   289
lift_definition signed :: \<open>'b::len word \<Rightarrow> 'a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   290
  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
   291
  by (simp flip: signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   292
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   293
lemma signed_0 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   294
  \<open>signed 0 = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   295
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   296
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   297
lemma signed_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   298
  \<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
   299
  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
   300
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   301
lemma signed_minus_1 [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   302
  \<open>signed (- 1 :: 'b::len word) = - 1\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   303
  by (transfer fixing: uminus) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   304
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   305
lemma signed_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   306
  \<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
   307
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   308
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   309
lemma signed_neg_numeral [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   310
  \<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
   311
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   312
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
   313
lemma signed_of_nat:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   314
  \<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
   315
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   316
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
   317
lemma signed_of_int:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   318
  \<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
   319
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   320
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   321
end
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
context ring_char_0
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   324
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   325
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   326
lemma signed_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   327
  \<open>v = w\<close> if \<open>signed v = signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   328
  using that by transfer (simp flip: signed_take_bit_decr_length_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   329
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   330
lemma word_eq_iff_signed:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   331
  \<open>v = w \<longleftrightarrow> signed v = signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   332
  by (auto intro: signed_word_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   333
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   334
lemma inj_signed [simp]:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   335
  \<open>inj signed\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   336
  by (rule injI) (simp add: signed_word_eqI)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
   337
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   338
lemma signed_eq_0_iff:
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   339
  \<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
   340
  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
   341
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   342
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   343
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   344
abbreviation unat :: \<open>'a::len word \<Rightarrow> nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   345
  where \<open>unat \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   346
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   347
abbreviation uint :: \<open>'a::len word \<Rightarrow> int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   348
  where \<open>uint \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   349
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   350
abbreviation sint :: \<open>'a::len word \<Rightarrow> int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   351
  where \<open>sint \<equiv> signed\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   352
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   353
abbreviation ucast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   354
  where \<open>ucast \<equiv> unsigned\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   355
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   356
abbreviation scast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   357
  where \<open>scast \<equiv> signed\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   358
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   359
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   360
  includes lifting_syntax
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   361
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   362
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   363
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   364
  \<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
   365
  using unsigned.transfer [where ?'a = nat] by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   366
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   367
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   368
  \<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
   369
  using unsigned.transfer [where ?'a = int] by (simp add: comp_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   370
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   371
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   372
  \<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
   373
  using signed.transfer [where ?'a = int] by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   374
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   375
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   376
  \<open>(pcr_word ===> pcr_word) (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
   377
proof (rule rel_funI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   378
  fix k :: int and w :: \<open>'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   379
  assume \<open>pcr_word k w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   380
  then have \<open>w = word_of_int k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   381
    by (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   382
  moreover have \<open>pcr_word (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
   383
    by transfer (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   384
  ultimately show \<open>pcr_word (take_bit LENGTH('a) k) (ucast w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   385
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   386
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   387
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   388
lemma [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   389
  \<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
   390
proof (rule rel_funI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   391
  fix k :: int and w :: \<open>'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   392
  assume \<open>pcr_word k w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   393
  then have \<open>w = word_of_int k\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   394
    by (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   395
  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
   396
    by transfer (simp add: pcr_word_def cr_word_def relcompp_apply)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   397
  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
   398
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   399
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   400
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   401
end
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
lemma of_nat_unat [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   404
  \<open>of_nat (unat w) = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   405
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   406
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   407
lemma of_int_uint [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   408
  \<open>of_int (uint w) = unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   409
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   410
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   411
lemma of_int_sint [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   412
  \<open>of_int (sint a) = signed a\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   413
  by transfer (simp_all add: take_bit_signed_take_bit)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   414
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   415
lemma nat_uint_eq [simp]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   416
  \<open>nat (uint w) = unat w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   417
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   418
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   419
lemma sgn_uint_eq [simp]:
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   420
  \<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
   421
  by transfer (simp add: less_le)
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
   422
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   423
text \<open>Aliasses only for code generation\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   424
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   425
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   426
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   427
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   428
qualified lift_definition of_int :: \<open>int \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   429
  is \<open>take_bit LENGTH('a)\<close> .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   430
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   431
qualified lift_definition of_nat :: \<open>nat \<Rightarrow> 'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   432
  is \<open>int \<circ> take_bit LENGTH('a)\<close> .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   433
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   434
qualified lift_definition the_nat :: \<open>'a::len word \<Rightarrow> nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   435
  is \<open>nat \<circ> take_bit LENGTH('a)\<close> by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   436
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   437
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
   438
  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
   439
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   440
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
   441
  is \<open>take_bit LENGTH('a)\<close> by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   442
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   443
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
   444
  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
   445
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   446
end
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
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   449
  \<open>Word.the_int = uint\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   450
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   451
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   452
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   453
  \<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
   454
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   455
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   456
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   457
  \<open>Word.of_int = word_of_int\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   458
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   459
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   460
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   461
  \<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
   462
  by transfer (simp add: take_bit_of_nat)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   463
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   464
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   465
  \<open>Word.of_nat = word_of_nat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   466
  by (rule; transfer) (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   467
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   468
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   469
  \<open>Word.the_nat w = nat (Word.the_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   470
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   471
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   472
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   473
  \<open>Word.the_nat = unat\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   474
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   475
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   476
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   477
  \<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
   478
  for w :: \<open>'a::len word\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
   479
  by transfer (simp add: signed_take_bit_take_bit)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   480
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   481
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   482
  \<open>Word.the_signed_int = sint\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   483
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   484
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   485
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   486
  \<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
   487
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   488
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   489
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   490
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   491
  \<open>Word.cast = ucast\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   492
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   493
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   494
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   495
  \<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
   496
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   497
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   498
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   499
lemma [code_abbrev, simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   500
  \<open>Word.signed_cast = scast\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   501
  by (rule; transfer) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   502
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   503
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   504
  \<open>unsigned w = of_nat (nat (Word.the_int w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   505
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   506
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   507
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   508
  \<open>signed w = of_int (Word.the_signed_int w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   509
  by transfer simp
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   510
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   511
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   512
subsubsection \<open>Basic ordering\<close>
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   513
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
   514
instantiation word :: (len) linorder
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   515
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   516
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   517
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   518
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   519
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   520
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   521
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   522
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   523
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   524
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   525
instance
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   526
  by (standard; transfer) auto
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   527
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   528
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   529
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   530
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
   531
  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
   532
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   533
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
   534
  by (standard; transfer) simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   535
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   536
lemma word_of_nat_less_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   537
  \<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
   538
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   539
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   540
lemma word_of_int_less_eq_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   541
  \<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
   542
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   543
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   544
lemma word_of_nat_less_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   545
  \<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
   546
  by transfer (simp add: take_bit_of_nat)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   547
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   548
lemma word_of_int_less_iff:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   549
  \<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
   550
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   551
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   552
lemma word_le_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   553
  "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   554
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   555
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   556
lemma word_less_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   557
  "a < b \<longleftrightarrow> uint a < uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   558
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   559
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   560
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
   561
  \<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
   562
  by transfer (simp add: less_le)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   563
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   564
lemma of_nat_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   565
  \<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
   566
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   567
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   568
lemma of_nat_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   569
  \<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
   570
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   571
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   572
lemma of_int_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   573
  \<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
   574
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   575
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   576
lemma of_int_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   577
  \<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
   578
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   579
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   580
72280
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   581
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   582
subsection \<open>Enumeration\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   583
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   584
lemma inj_on_word_of_nat:
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   585
  \<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
   586
  by (rule inj_onI; transfer) (simp_all add: take_bit_int_eq_self)
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   587
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   588
lemma UNIV_word_eq_word_of_nat:
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   589
  \<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
   590
proof
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   591
  show \<open>word_of_nat ` {0..<2 ^ LENGTH('a)} \<subseteq> UNIV\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   592
    by simp
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   593
  show \<open>UNIV \<subseteq> ?A\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   594
  proof
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   595
    fix w :: \<open>'a word\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   596
    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
   597
      by (rule image_eqI [of _ _ \<open>unat w\<close>]; transfer) simp_all
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   598
  qed
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   599
qed
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   600
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   601
instantiation word :: (len) enum
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   602
begin
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   603
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   604
definition enum_word :: \<open>'a word list\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   605
  where \<open>enum_word = map word_of_nat [0..<2 ^ LENGTH('a)]\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   606
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   607
definition enum_all_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close>
77225
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   608
  where \<open>enum_all_word = All\<close>
72280
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   609
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   610
definition enum_ex_word :: \<open>('a word \<Rightarrow> bool) \<Rightarrow> bool\<close>
77225
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   611
  where \<open>enum_ex_word = Ex\<close>
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   612
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   613
instance
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   614
  by standard
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   615
    (simp_all add: enum_all_word_def enum_ex_word_def enum_word_def distinct_map inj_on_word_of_nat flip: UNIV_word_eq_word_of_nat)
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   616
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   617
end
72280
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   618
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   619
lemma [code]:
77225
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   620
  \<open>Enum.enum_all P \<longleftrightarrow> list_all P Enum.enum\<close>
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   621
  \<open>Enum.enum_ex P \<longleftrightarrow> list_ex P Enum.enum\<close> for P :: \<open>'a::len word \<Rightarrow> bool\<close>
b6f3eb537d91 actually executable enum_all, enum_ex for word
haftmann
parents: 77061
diff changeset
   622
  by (simp_all add: enum_all_word_def enum_ex_word_def enum_UNIV list_all_iff list_ex_iff)
72280
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   623
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
   624
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   625
subsection \<open>Bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   626
77812
fb3d81bd9803 some remarks on division
haftmann
parents: 77225
diff changeset
   627
text \<open>
fb3d81bd9803 some remarks on division
haftmann
parents: 77225
diff changeset
   628
  The following specification of word division just lifts the pre-existing
fb3d81bd9803 some remarks on division
haftmann
parents: 77225
diff changeset
   629
  division on integers named ``F-Division'' in \cite{leijen01}.
fb3d81bd9803 some remarks on division
haftmann
parents: 77225
diff changeset
   630
\<close>
fb3d81bd9803 some remarks on division
haftmann
parents: 77225
diff changeset
   631
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   632
instantiation word :: (len) semiring_modulo
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   633
begin
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   634
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   635
lift_definition divide_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 div 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
lift_definition modulo_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   640
  is \<open>\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   641
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   642
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   643
instance proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   644
  show "a div b * b + a mod b = a" for a b :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   645
  proof transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   646
    fix k l :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   647
    define r :: int where "r = 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   648
    then have r: "take_bit LENGTH('a) k = k mod r" for k
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   649
      by (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   650
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   651
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   652
      by (simp add: div_mult_mod_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   653
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   654
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   655
      by (simp add: mod_add_left_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   656
    also have "... = (((k mod r) div (l mod r) * l) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   657
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   658
      by (simp add: mod_mult_right_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   659
    finally have "k mod r = ((k mod r) div (l mod r) * l
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   660
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   661
      by (simp add: mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   662
    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
   663
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   664
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   665
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   666
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   667
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   668
end
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   669
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   670
instance word :: (len) semiring_parity
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   671
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   672
  show "\<not> 2 dvd (1::'a word)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   673
    by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   674
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   675
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   676
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   677
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   678
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   679
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   680
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   681
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   682
lemma word_bit_induct [case_names zero even odd]:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   683
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   684
    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
   685
    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
   686
  for P and a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   687
proof -
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   688
  define m :: nat where \<open>m = LENGTH('a) - Suc 0\<close>
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   689
  then have l: \<open>LENGTH('a) = Suc m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   690
    by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   691
  define n :: nat where \<open>n = unat a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   692
  then have \<open>n < 2 ^ LENGTH('a)\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   693
    by transfer (simp add: take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   694
  then have \<open>n < 2 * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   695
    by (simp add: l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   696
  then have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   697
  proof (induction n rule: nat_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   698
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   699
    show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   700
      by simp (rule word_zero)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   701
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   702
    case (even n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   703
    then have \<open>n < 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   704
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   705
    with even.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   706
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   707
    moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close>
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
   708
      by (auto simp add: word_greater_zero_iff l word_of_nat_eq_0_iff)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   709
    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
   710
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   711
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   712
    ultimately have \<open>P (2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   713
      by (rule word_even)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   714
    then show ?case
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
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   717
    case (odd n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   718
    then have \<open>Suc n \<le> 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   719
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   720
    with odd.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   721
      by simp
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   722
    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
   723
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   724
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   725
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   726
      by (rule word_odd)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   727
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   728
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   729
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   730
  moreover have \<open>of_nat (nat (uint a)) = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   731
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   732
  ultimately show ?thesis
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   733
    by (simp add: n_def)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   734
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   735
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   736
lemma bit_word_half_eq:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   737
  \<open>(of_bool b + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   738
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   739
    for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   740
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   741
  case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   742
  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
   743
    by auto
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   744
  with False that show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   745
    by transfer (simp add: eq_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   746
next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   747
  case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   748
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   749
    by (cases \<open>LENGTH('a)\<close>) simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   750
  show ?thesis proof (cases b)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   751
    case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   752
    moreover have \<open>a * 2 div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   753
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   754
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   755
      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
   756
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   757
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
76231
8a48e18f081e reduce prominence of facts
haftmann
parents: 75623
diff changeset
   758
      with \<open>LENGTH('a) = Suc n\<close> have \<open>take_bit LENGTH('a) k = take_bit n k\<close>
8a48e18f081e reduce prominence of facts
haftmann
parents: 75623
diff changeset
   759
        by (auto simp add: take_bit_Suc_from_most)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   760
      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
   761
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   762
      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
   763
        = take_bit LENGTH('a) k\<close>
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
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   766
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   767
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   768
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   769
    case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   770
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   771
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   772
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   773
      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
   774
        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
   775
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
76231
8a48e18f081e reduce prominence of facts
haftmann
parents: 75623
diff changeset
   776
      with \<open>LENGTH('a) = Suc n\<close> have \<open>take_bit LENGTH('a) k = take_bit n k\<close>
8a48e18f081e reduce prominence of facts
haftmann
parents: 75623
diff changeset
   777
        by (auto simp add: take_bit_Suc_from_most)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   778
      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
   779
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   780
      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
   781
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   782
        by (auto simp add: take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   783
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   784
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   785
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   786
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   787
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   788
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   789
lemma even_mult_exp_div_word_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   790
  \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> (
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   791
    m \<le> n \<and>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   792
    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
   793
  by transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   794
    (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
   795
      simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   796
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   797
instantiation word :: (len) semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   798
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   799
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   800
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
   801
  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
   802
proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   803
  fix k l :: int and n :: nat
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   804
  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
   805
  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
   806
  proof (cases \<open>n < LENGTH('a)\<close>)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   807
    case True
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   808
    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
   809
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   810
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   811
      by (simp add: bit_take_bit_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   812
  next
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   813
    case False
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   814
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   815
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   816
  qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   817
qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   818
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   819
instance proof
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   820
  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
   821
    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
   822
  for P and a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   823
  proof (induction a rule: word_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   824
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   825
    have \<open>0 div 2 = (0::'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   826
      by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   827
    with stable [of 0] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   828
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   829
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   830
    case (even a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   831
    with rec [of a False] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   832
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   833
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   834
    case (odd a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   835
    with rec [of a True] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   836
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   837
  qed
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   838
  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
   839
    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
   840
  show \<open>0 div a = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   841
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   842
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   843
  show \<open>a div 1 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   844
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   845
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   846
  show \<open>a mod b div b = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   847
    for a b :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   848
    apply transfer
77061
5de3772609ea generalized theory name: euclidean division denotes one particular division definition on integers
haftmann
parents: 76231
diff changeset
   849
    apply (simp add: take_bit_eq_mod)
5de3772609ea generalized theory name: euclidean division denotes one particular division definition on integers
haftmann
parents: 76231
diff changeset
   850
    apply (smt (verit, best) Euclidean_Rings.pos_mod_bound Euclidean_Rings.pos_mod_sign div_int_pos_iff
5de3772609ea generalized theory name: euclidean division denotes one particular division definition on integers
haftmann
parents: 76231
diff changeset
   851
        nonneg1_imp_zdiv_pos_iff zero_less_power zmod_le_nonneg_dividend)
5de3772609ea generalized theory name: euclidean division denotes one particular division definition on integers
haftmann
parents: 76231
diff changeset
   852
    done
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
73535
0f33c7031ec9 new lemmas
haftmann
parents: 72954
diff changeset
   857
      (auto dest: le_Suc_ex simp add: 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
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
   881
    by transfer
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
   882
      (simp flip: drop_bit_eq_div mask_eq_exp_minus_1 add: bit_simps even_drop_bit_iff_not_bit not_less)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   883
  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
   884
    for a :: \<open>'a word\<close> and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   885
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   886
    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
   887
      n < m
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   888
      \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   889
      \<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
   890
    for m n :: nat and k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   891
      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
   892
        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
   893
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   894
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   895
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   896
end
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   897
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   898
lemma bit_word_eqI:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   899
  \<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
   900
  for a b :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   901
  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
   902
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   903
lemma bit_imp_le_length:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   904
  \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   905
    for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   906
  using that by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   907
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   908
lemma not_bit_length [simp]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   909
  \<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
   910
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   911
72830
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   912
lemma finite_bit_word [simp]:
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   913
  \<open>finite {n. bit w n}\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   914
  for w :: \<open>'a::len word\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   915
proof -
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   916
  have \<open>{n. bit w n} \<subseteq> {0..LENGTH('a)}\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   917
    by (auto dest: bit_imp_le_length)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   918
  moreover have \<open>finite {0..LENGTH('a)}\<close>
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   919
    by simp
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   920
  ultimately show ?thesis
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   921
    by (rule finite_subset)
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   922
qed
ec0d3a62bb3b moved some lemmas from AFP to distribution
haftmann
parents: 72735
diff changeset
   923
73789
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   924
lemma bit_numeral_word_iff [simp]:
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   925
  \<open>bit (numeral w :: 'a::len word) n
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   926
    \<longleftrightarrow> n < LENGTH('a) \<and> bit (numeral w :: int) n\<close>
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   927
  by transfer simp
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   928
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   929
lemma bit_neg_numeral_word_iff [simp]:
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   930
  \<open>bit (- numeral w :: 'a::len word) n
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   931
    \<longleftrightarrow> n < LENGTH('a) \<and> bit (- numeral w :: int) n\<close>
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   932
  by transfer simp
aab7975fa070 more lemmas
haftmann
parents: 73788
diff changeset
   933
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   934
instantiation word :: (len) ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   935
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   936
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   937
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   938
  is not
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   939
  by (simp add: take_bit_not_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   940
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   941
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
   942
  is \<open>and\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   943
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   944
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   945
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
   946
  is or
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   947
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   948
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   949
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
   950
  is xor
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   951
  by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   952
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   953
lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   954
  is mask
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   955
  .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
   956
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   957
lift_definition set_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   958
  is set_bit
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   959
  by (simp add: set_bit_def)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   960
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   961
lift_definition unset_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   962
  is unset_bit
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   963
  by (simp add: unset_bit_def)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   964
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   965
lift_definition flip_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   966
  is flip_bit
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   967
  by (simp add: flip_bit_def)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
   968
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   969
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   970
  is push_bit
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   971
proof -
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   972
  show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   973
    if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   974
  proof -
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   975
    from that
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   976
    have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   977
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   978
      by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   979
    moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   980
      by simp
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   981
    ultimately show ?thesis
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   982
      by (simp add: take_bit_push_bit)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   983
  qed
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   984
qed
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   985
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   986
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   987
  is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   988
  by (simp add: take_bit_eq_mod)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   989
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   990
lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   991
  is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   992
  by (simp add: ac_simps) (simp only: flip: take_bit_take_bit)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
   993
79008
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   994
context
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   995
  includes bit_operations_syntax
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   996
begin
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   997
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   998
instance proof
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
   999
  fix v w :: \<open>'a word\<close> and n m :: nat
79018
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1000
  show \<open>NOT v = of_bool (even v) + 2 * NOT (v div 2)\<close>
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1001
    apply transfer
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1002
    apply (rule bit_eqI)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1003
    apply (auto simp add: even_bit_succ_iff bit_simps bit_0 simp flip: bit_Suc)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1004
     apply (metis Suc_pred bit_0 not_gr_zero)
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1005
    using odd_bit_iff_bit_pred apply blast
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1006
    done
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1007
  show \<open>- v = NOT (v - 1)\<close>
7449ff77029e base abstract specification of NOT on recursive equation rather than bit projection
haftmann
parents: 79008
diff changeset
  1008
    by transfer (simp add: minus_eq_not_minus_1)
79008
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1009
  show \<open>v AND w = of_bool (odd v \<and> odd w) + 2 * (v div 2 AND w div 2)\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1010
    apply transfer
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1011
    apply (rule bit_eqI)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1012
    apply (auto simp add: even_bit_succ_iff bit_simps bit_0 simp flip: bit_Suc)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1013
    done
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1014
  show \<open>v OR w = of_bool (odd v \<or> odd w) + 2 * (v div 2 OR w div 2)\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1015
    apply transfer
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1016
    apply (rule bit_eqI)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1017
    apply (auto simp add: even_bit_succ_iff bit_simps bit_0 simp flip: bit_Suc)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1018
    done
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1019
  show \<open>v XOR w = of_bool (odd v \<noteq> odd w) + 2 * (v div 2 XOR w div 2)\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1020
    apply transfer
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1021
    apply (rule bit_eqI)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1022
    subgoal for k l n
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1023
      apply (cases n)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1024
       apply (auto simp add: even_bit_succ_iff bit_simps bit_0 even_xor_iff simp flip: bit_Suc)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1025
      done
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1026
    done
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1027
  show \<open>mask n = 2 ^ n - (1 :: 'a word)\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1028
    by transfer (simp flip: mask_eq_exp_minus_1)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1029
  show \<open>set_bit n v = v OR push_bit n 1\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1030
    by transfer (simp add: take_bit_set_bit_eq set_bit_eq_or)
79031
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1031
  show \<open>unset_bit 0 v = 2 * (v div 2)\<close>
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1032
    apply transfer
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1033
    apply (rule bit_eqI)
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1034
    apply (auto simp add: bit_simps simp flip: bit_Suc)
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1035
    done
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1036
  show \<open>unset_bit (Suc n) v = v mod 2 + 2 * unset_bit n (v div 2)\<close>
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1037
    apply transfer
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1038
    apply (rule bit_eqI)
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1039
    apply (auto simp add: bit_simps mod_2_eq_odd even_bit_succ_iff bit_0 simp flip: bit_Suc)
4596a14d9a95 slightly more elementary characterization of unset_bit
haftmann
parents: 79018
diff changeset
  1040
    done
79008
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1041
  show \<open>flip_bit n v = v XOR push_bit n 1\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1042
    by transfer (simp add: take_bit_flip_bit_eq flip_bit_eq_xor)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1043
  show \<open>push_bit n v = v * 2 ^ n\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1044
    by transfer (simp add: push_bit_eq_mult)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1045
  show \<open>drop_bit n v = v div 2 ^ n\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1046
    by transfer (simp add: drop_bit_take_bit flip: drop_bit_eq_div)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1047
  show \<open>take_bit n v = v mod 2 ^ n\<close>
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1048
    by transfer (simp flip: take_bit_eq_mod)
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1049
qed
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1050
74a4776f7a22 operations AND, OR, XOR are specified by characteristic recursive equation
haftmann
parents: 78955
diff changeset
  1051
end
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1052
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1053
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1054
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1055
lemma [code]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1056
  \<open>push_bit n w = w * 2 ^ n\<close> for w :: \<open>'a::len word\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1057
  by (fact push_bit_eq_mult)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1058
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1059
lemma [code]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1060
  \<open>Word.the_int (drop_bit n w) = drop_bit n (Word.the_int w)\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1061
  by transfer (simp add: drop_bit_take_bit min_def le_less less_diff_conv)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1062
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1063
lemma [code]:
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1064
  \<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>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1065
  for w :: \<open>'a::len word\<close>
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1066
  by transfer (simp add: not_le not_less ac_simps min_absorb2)
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1067
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1068
lemma [code_abbrev]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1069
  \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1070
  by (fact push_bit_of_1)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1071
74391
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1072
context
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1073
  includes bit_operations_syntax
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1074
begin
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1075
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1076
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1077
  \<open>NOT w = Word.of_int (NOT (Word.the_int w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1078
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1079
  by transfer (simp add: take_bit_not_take_bit) 
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1080
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1081
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1082
  \<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
  1083
  by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1084
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1085
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1086
  \<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
  1087
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1088
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1089
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1090
  \<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
  1091
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1092
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1093
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1094
  \<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
  1095
  by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1096
73682
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1097
lemma [code]:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1098
  \<open>set_bit n w = w OR push_bit n 1\<close> for w :: \<open>'a::len word\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1099
  by (fact set_bit_eq_or)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1100
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1101
lemma [code]:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1102
  \<open>unset_bit n w = w AND NOT (push_bit n 1)\<close> for w :: \<open>'a::len word\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1103
  by (fact unset_bit_eq_and_not)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1104
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1105
lemma [code]:
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1106
  \<open>flip_bit n w = w XOR push_bit n 1\<close> for w :: \<open>'a::len word\<close>
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1107
  by (fact flip_bit_eq_xor)
78044b2f001c explicit type class operations for type-specific implementations
haftmann
parents: 73535
diff changeset
  1108
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1109
context
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1110
  includes lifting_syntax
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1111
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1112
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1113
lemma set_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1114
  \<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1115
  by (unfold set_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1116
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1117
lemma unset_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1118
  \<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1119
  by (unfold unset_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1120
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1121
lemma flip_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1122
  \<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1123
  by (unfold flip_bit_def) transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1124
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1125
lemma signed_take_bit_word_transfer [transfer_rule]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1126
  \<open>((=) ===> pcr_word ===> pcr_word)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1127
    (\<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
  1128
    (signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1129
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1130
  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
  1131
  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
  1132
  have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1133
    by transfer_prover
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1134
  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
  1135
    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
  1136
  also have \<open>?W = signed_take_bit\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1137
    by (simp add: fun_eq_iff signed_take_bit_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1138
  finally show ?thesis .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1139
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1140
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1141
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1142
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1143
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1144
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1145
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1146
subsection \<open>Conversions including casts\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1147
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1148
subsubsection \<open>Generic unsigned conversion\<close>
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 semiring_bits
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
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  1153
lemma bit_unsigned_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1154
  \<open>bit (unsigned w) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit w n\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1155
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1156
  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
  1157
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1158
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1159
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1160
lemma possible_bit_word[simp]:
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1161
  \<open>possible_bit TYPE(('a :: len) word) m \<longleftrightarrow> m < LENGTH('a)\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1162
  by (simp add: possible_bit_def linorder_not_le)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1163
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1164
context semiring_bit_operations
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1165
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1166
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1167
lemma unsigned_minus_1_eq_mask:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1168
  \<open>unsigned (- 1 :: 'b::len word) = mask LENGTH('b)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1169
  by (transfer fixing: mask) (simp add: nat_mask_eq of_nat_mask_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1170
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1171
lemma unsigned_push_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1172
  \<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
  1173
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1174
proof (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1175
  fix m
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1176
  assume \<open>possible_bit TYPE('a) m\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1177
  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
  1178
  proof (cases \<open>n \<le> m\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1179
    case True
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1180
    with \<open>possible_bit TYPE('a) m\<close> have \<open>possible_bit TYPE('a) (m - n)\<close>
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1181
      by (simp add: possible_bit_less_imp)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1182
    with True show ?thesis
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1183
      by (simp add: bit_unsigned_iff bit_push_bit_iff Bit_Operations.bit_push_bit_iff bit_take_bit_iff not_le ac_simps)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1184
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1185
    case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1186
    then show ?thesis
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1187
      by (simp add: not_le bit_unsigned_iff bit_push_bit_iff Bit_Operations.bit_push_bit_iff bit_take_bit_iff)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1188
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1189
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1190
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1191
lemma unsigned_take_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1192
  \<open>unsigned (take_bit n w) = take_bit n (unsigned w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1193
  for w :: \<open>'b::len word\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1194
  by (rule bit_eqI) (simp add: bit_unsigned_iff bit_take_bit_iff Bit_Operations.bit_take_bit_iff)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1195
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1196
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1197
78955
74147aa81dbb more specific name for type class
haftmann
parents: 77812
diff changeset
  1198
context linordered_euclidean_semiring_bit_operations
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1199
begin
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1200
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1201
lemma unsigned_drop_bit_eq:
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1202
  \<open>unsigned (drop_bit n w) = drop_bit n (take_bit LENGTH('b) (unsigned w))\<close>
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1203
  for w :: \<open>'b::len word\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1204
  by (rule bit_eqI) (auto simp add: bit_unsigned_iff bit_take_bit_iff bit_drop_bit_eq Bit_Operations.bit_drop_bit_eq possible_bit_def dest: bit_imp_le_length)
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1205
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1206
end
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  1207
73853
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  1208
lemma ucast_drop_bit_eq:
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  1209
  \<open>ucast (drop_bit n w) = drop_bit n (ucast w :: 'b::len word)\<close>
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  1210
  if \<open>LENGTH('a) \<le> LENGTH('b)\<close> for w :: \<open>'a::len word\<close>
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  1211
  by (rule bit_word_eqI) (use that in \<open>auto simp add: bit_unsigned_iff bit_drop_bit_eq dest: bit_imp_le_length\<close>)
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  1212
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1213
context semiring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1214
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1215
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1216
context
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1217
  includes bit_operations_syntax
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1218
begin
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1219
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1220
lemma unsigned_and_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1221
  \<open>unsigned (v AND w) = unsigned v AND unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1222
  for v w :: \<open>'b::len word\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1223
  by (simp add: bit_eq_iff bit_simps)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1224
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1225
lemma unsigned_or_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1226
  \<open>unsigned (v OR w) = unsigned v OR unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1227
  for v w :: \<open>'b::len word\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1228
  by (simp add: bit_eq_iff bit_simps)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1229
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1230
lemma unsigned_xor_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1231
  \<open>unsigned (v XOR w) = unsigned v XOR unsigned w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1232
  for v w :: \<open>'b::len word\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1233
  by (simp add: bit_eq_iff bit_simps)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1234
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1235
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1236
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1237
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1238
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1239
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1240
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1241
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1242
context
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1243
  includes bit_operations_syntax
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1244
begin
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1245
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1246
lemma unsigned_not_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1247
  \<open>unsigned (NOT w) = take_bit LENGTH('b) (NOT (unsigned w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1248
  for w :: \<open>'b::len word\<close>
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1249
  by (simp add: bit_eq_iff bit_simps)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1250
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1251
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1252
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1253
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1254
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1255
context unique_euclidean_semiring_numeral
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1256
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1257
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  1258
lemma unsigned_greater_eq [simp]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1259
  \<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
  1260
  by (transfer fixing: less_eq) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1261
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  1262
lemma unsigned_less [simp]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1263
  \<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
  1264
  by (transfer fixing: less) simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1265
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1266
end
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
context linordered_semidom
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1269
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1270
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1271
lemma word_less_eq_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1272
  "a \<le> b \<longleftrightarrow> unsigned a \<le> unsigned b"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1273
  by (transfer fixing: less_eq) (simp add: nat_le_eq_zle)
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
lemma word_less_iff_unsigned:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1276
  "a < b \<longleftrightarrow> unsigned a < unsigned b"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1277
  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
  1278
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1279
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1280
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1281
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1282
subsubsection \<open>Generic signed conversion\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1283
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1284
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1285
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1286
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  1287
lemma bit_signed_iff [bit_simps]:
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1288
  \<open>bit (signed w) n \<longleftrightarrow> possible_bit TYPE('a) n \<and> bit w (min (LENGTH('b) - Suc 0) n)\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1289
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1290
  by (transfer fixing: bit)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1291
    (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
  1292
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1293
lemma signed_push_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1294
  \<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
  1295
  for w :: \<open>'b::len word\<close>
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  1296
  apply (simp add: bit_eq_iff bit_simps possible_bit_less_imp min_less_iff_disj)
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1297
  apply (cases n, simp_all add: min_def)
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1298
  done
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1299
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1300
lemma signed_take_bit_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1301
  \<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
  1302
  for w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1303
  by (transfer fixing: take_bit; cases \<open>LENGTH('b)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1304
    (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
  1305
74391
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1306
context
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1307
  includes bit_operations_syntax
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1308
begin
930047942f46 repaired slip
haftmann
parents: 74309
diff changeset
  1309
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1310
lemma signed_not_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1311
  \<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
  1312
  for w :: \<open>'b::len word\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1313
  by (simp add: bit_eq_iff bit_simps possible_bit_less_imp min_less_iff_disj)
74309
42523fbf643b explicit predicate for confined bit range avoids cyclic rewriting in presence of extensionality rule for bit values (contributed by Thomas Sewell)
haftmann
parents: 74163
diff changeset
  1314
    (auto simp: min_def)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1315
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1316
lemma signed_and_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1317
  \<open>signed (v AND w) = signed v AND signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1318
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1319
  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
  1320
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1321
lemma signed_or_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1322
  \<open>signed (v OR w) = signed v OR signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1323
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1324
  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
  1325
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1326
lemma signed_xor_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1327
  \<open>signed (v XOR w) = signed v XOR signed w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1328
  for v w :: \<open>'b::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1329
  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
  1330
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1331
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1332
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1333
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1334
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1335
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1336
subsubsection \<open>More\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1337
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1338
lemma sint_greater_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1339
  \<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
  1340
proof (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1341
  case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1342
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1343
    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
  1344
next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1345
  have *: \<open>- (2 ^ (LENGTH('a) - Suc 0)) \<le> (0::int)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1346
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1347
  case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1348
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1349
    by transfer (auto simp add: signed_take_bit_eq intro: order_trans *)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1350
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1351
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1352
lemma sint_less:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1353
  \<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
  1354
  by (cases \<open>bit w (LENGTH('a) - Suc 0)\<close>; transfer)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1355
    (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
  1356
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1357
lemma unat_div_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1358
  \<open>unat (v div w) = unat v div unat w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1359
proof transfer
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1360
  fix k l
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1361
  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
  1362
    by (rule div_le_dividend)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1363
  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
  1364
    by (simp add: nat_less_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1365
  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
  1366
    (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
  1367
    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
  1368
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1369
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1370
lemma unat_mod_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1371
  \<open>unat (v mod w) = unat v mod unat w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1372
proof transfer
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1373
  fix k l
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1374
  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
  1375
    by (rule mod_less_eq_dividend)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1376
  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
  1377
    by (simp add: nat_less_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1378
  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
  1379
    (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
  1380
    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
  1381
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1382
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1383
lemma uint_div_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1384
  \<open>uint (v div w) = uint v div uint w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1385
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1386
  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
  1387
    by (simp add: unat_div_distrib)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1388
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1389
    by (simp add: of_nat_div)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1390
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1391
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1392
lemma unat_drop_bit_eq:
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1393
  \<open>unat (drop_bit n w) = drop_bit n (unat w)\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1394
  by (rule bit_eqI) (simp add: bit_unsigned_iff bit_drop_bit_eq)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1395
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1396
lemma uint_mod_distrib:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1397
  \<open>uint (v mod w) = uint v mod uint w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1398
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1399
  have \<open>int (unat (v mod w)) = int (unat v mod unat w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1400
    by (simp add: unat_mod_distrib)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1401
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1402
    by (simp add: of_nat_mod)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1403
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1404
74108
3146646a43a7 simplified hierarchy of type classes for bit operations
haftmann
parents: 74101
diff changeset
  1405
context semiring_bit_operations
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1406
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1407
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1408
lemma unsigned_ucast_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1409
  \<open>unsigned (ucast w :: 'c::len word) = take_bit LENGTH('c) (unsigned w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1410
  for w :: \<open>'b::len word\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  1411
  by (rule bit_eqI) (simp add: bit_unsigned_iff Word.bit_unsigned_iff bit_take_bit_iff not_le)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1412
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1413
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1414
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1415
context ring_bit_operations
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1416
begin
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1417
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1418
lemma signed_ucast_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1419
  \<open>signed (ucast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (unsigned w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1420
  for w :: \<open>'b::len word\<close>
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1421
  by (simp add: bit_eq_iff bit_simps min_less_iff_disj)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1422
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1423
lemma signed_scast_eq:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1424
  \<open>signed (scast w :: 'c::len word) = signed_take_bit (LENGTH('c) - Suc 0) (signed w)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1425
  for w :: \<open>'b::len word\<close>
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  1426
  by (simp add: bit_eq_iff bit_simps min_less_iff_disj)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1427
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1428
end
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1429
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1430
lemma uint_nonnegative: "0 \<le> uint w"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1431
  by (fact unsigned_greater_eq)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1432
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1433
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1434
  for w :: "'a::len word"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1435
  by (fact unsigned_less)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1436
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1437
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1438
  for w :: "'a::len word"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1439
  by transfer (simp add: take_bit_eq_mod)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1440
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1441
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1442
  by (fact unsigned_word_eqI)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1443
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1444
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1445
  by (fact word_eq_iff_unsigned)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1446
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1447
lemma uint_word_of_int_eq:
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1448
  \<open>uint (word_of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1449
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1450
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1451
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1452
  by (simp add: uint_word_of_int_eq take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1453
  
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1454
lemma word_of_int_uint: "word_of_int (uint w) = w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1455
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1456
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1457
lemma word_div_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1458
  "a div b = word_of_int (uint a div uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1459
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1460
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1461
lemma word_mod_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1462
  "a mod b = word_of_int (uint a mod uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1463
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1464
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1465
lemma split_word_all: "(\<And>x::'a::len word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1466
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1467
  fix x :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1468
  assume "\<And>x. PROP P (word_of_int x)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1469
  then have "PROP P (word_of_int (uint x))" .
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1470
  then show "PROP P x"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1471
    by (simp only: word_of_int_uint)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1472
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1473
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1474
lemma sint_uint:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1475
  \<open>sint w = signed_take_bit (LENGTH('a) - Suc 0) (uint w)\<close>
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1476
  for w :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1477
  by (cases \<open>LENGTH('a)\<close>; transfer) (simp_all add: signed_take_bit_take_bit)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1478
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1479
lemma unat_eq_nat_uint:
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1480
  \<open>unat w = nat (uint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1481
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1482
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1483
lemma ucast_eq:
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1484
  \<open>ucast w = word_of_int (uint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1485
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1486
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1487
lemma scast_eq:
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1488
  \<open>scast w = word_of_int (sint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1489
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1490
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1491
lemma uint_0_eq:
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1492
  \<open>uint 0 = 0\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1493
  by (fact unsigned_0)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1494
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1495
lemma uint_1_eq:
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1496
  \<open>uint 1 = 1\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1497
  by (fact unsigned_1)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1498
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1499
lemma word_m1_wi: "- 1 = word_of_int (- 1)"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1500
  by simp
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1501
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1502
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1503
  by (auto simp add: unsigned_word_eqI)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1504
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1505
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1506
  by (auto simp add: unsigned_word_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1507
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1508
lemma unat_0: "unat 0 = 0"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1509
  by (fact unsigned_0)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1510
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1511
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1512
  by (auto simp: unat_0_iff [symmetric])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1513
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1514
lemma ucast_0: "ucast 0 = 0"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1515
  by (fact unsigned_0)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1516
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1517
lemma sint_0: "sint 0 = 0"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1518
  by (fact signed_0)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1519
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1520
lemma scast_0: "scast 0 = 0"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1521
  by (fact signed_0)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1522
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1523
lemma sint_n1: "sint (- 1) = - 1"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1524
  by (fact signed_minus_1)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1525
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1526
lemma scast_n1: "scast (- 1) = - 1"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1527
  by (fact signed_minus_1)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1528
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1529
lemma uint_1: "uint (1::'a::len word) = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1530
  by (fact uint_1_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1531
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1532
lemma unat_1: "unat (1::'a::len word) = 1"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1533
  by (fact unsigned_1)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1534
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1535
lemma ucast_1: "ucast (1::'a::len word) = 1"
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1536
  by (fact unsigned_1)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1537
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1538
instantiation word :: (len) size
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1539
begin
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1540
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1541
lift_definition size_word :: \<open>'a word \<Rightarrow> nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1542
  is \<open>\<lambda>_. LENGTH('a)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1543
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1544
instance ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1545
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1546
end
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1547
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1548
lemma word_size [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1549
  \<open>size w = LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1550
  by (fact size_word.rep_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1551
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1552
lemma word_size_gt_0 [iff]: "0 < size w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1553
  for w :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1554
  by (simp add: word_size)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1555
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1556
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1557
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1558
lemma lens_not_0 [iff]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1559
  \<open>size w \<noteq> 0\<close> for  w :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1560
  by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1561
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1562
lift_definition source_size :: \<open>('a::len word \<Rightarrow> 'b) \<Rightarrow> nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1563
  is \<open>\<lambda>_. LENGTH('a)\<close> .
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1564
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1565
lift_definition target_size :: \<open>('a \<Rightarrow> 'b::len word) \<Rightarrow> nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1566
  is \<open>\<lambda>_. LENGTH('b)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1567
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1568
lift_definition is_up :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1569
  is \<open>\<lambda>_. LENGTH('a) \<le> LENGTH('b)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1570
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1571
lift_definition is_down :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1572
  is \<open>\<lambda>_. LENGTH('a) \<ge> LENGTH('b)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1573
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1574
lemma is_up_eq:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1575
  \<open>is_up f \<longleftrightarrow> source_size f \<le> target_size f\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1576
  for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1577
  by (simp add: source_size.rep_eq target_size.rep_eq is_up.rep_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1578
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1579
lemma is_down_eq:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1580
  \<open>is_down f \<longleftrightarrow> target_size f \<le> source_size f\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1581
  for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1582
  by (simp add: source_size.rep_eq target_size.rep_eq is_down.rep_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1583
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1584
lift_definition word_int_case :: \<open>(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1585
  is \<open>\<lambda>f. f \<circ> take_bit LENGTH('a)\<close> by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1586
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1587
lemma word_int_case_eq_uint [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1588
  \<open>word_int_case f w = f (uint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1589
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1590
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1591
translations
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1592
  "case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1593
  "case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1594
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1595
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1596
subsection \<open>Arithmetic operations\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1597
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1598
lemma div_word_self:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1599
  \<open>w div w = 1\<close> if \<open>w \<noteq> 0\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1600
  using that by transfer simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1601
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1602
lemma mod_word_self [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1603
  \<open>w mod w = 0\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1604
  apply (cases \<open>w = 0\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1605
  apply auto
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1606
  using div_mult_mod_eq [of w w] by (simp add: div_word_self)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1607
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1608
lemma div_word_less:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1609
  \<open>w div v = 0\<close> if \<open>w < v\<close> for w v :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1610
  using that by transfer simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1611
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1612
lemma mod_word_less:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1613
  \<open>w mod v = w\<close> if \<open>w < v\<close> for w v :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1614
  using div_mult_mod_eq [of w v] using that by (simp add: div_word_less)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1615
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1616
lemma div_word_one [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1617
  \<open>1 div w = of_bool (w = 1)\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1618
proof transfer
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1619
  fix k :: int
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1620
  show \<open>take_bit LENGTH('a) (take_bit LENGTH('a) 1 div take_bit LENGTH('a) k) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1621
         take_bit LENGTH('a) (of_bool (take_bit LENGTH('a) k = take_bit LENGTH('a) 1))\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1622
  proof (cases \<open>take_bit LENGTH('a) k > 1\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1623
    case False
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1624
    with take_bit_nonnegative [of \<open>LENGTH('a)\<close> k]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1625
    have \<open>take_bit LENGTH('a) k = 0 \<or> take_bit LENGTH('a) k = 1\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1626
      by linarith
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1627
    then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1628
      by auto
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1629
  next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1630
    case True
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1631
    then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1632
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1633
  qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1634
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1635
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1636
lemma mod_word_one [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1637
  \<open>1 mod w = 1 - w * of_bool (w = 1)\<close> for w :: \<open>'a::len word\<close>
75087
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1638
  using div_mult_mod_eq [of 1 w] by auto
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1639
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1640
lemma div_word_by_minus_1_eq [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1641
  \<open>w div - 1 = of_bool (w = - 1)\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1642
  by (auto intro: div_word_less simp add: div_word_self word_order.not_eq_extremum)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1643
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1644
lemma mod_word_by_minus_1_eq [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1645
  \<open>w mod - 1 = w * of_bool (w < - 1)\<close> for w :: \<open>'a::len word\<close>
75087
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1646
proof (cases \<open>w = - 1\<close>)
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1647
  case True
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1648
  then show ?thesis
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1649
    by simp
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1650
next
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1651
  case False
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1652
  moreover have \<open>w < - 1\<close>
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1653
    using False by (simp add: word_order.not_eq_extremum)
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1654
  ultimately show ?thesis
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1655
    by (simp add: mod_word_less)
f3fcc7c5a0db Avoid overaggresive splitting.
haftmann
parents: 75085
diff changeset
  1656
qed
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1657
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1658
text \<open>Legacy theorems:\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1659
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1660
lemma word_add_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1661
  "a + b = word_of_int (uint a + uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1662
  by transfer (simp add: take_bit_add)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1663
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1664
lemma word_sub_wi [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1665
  "a - b = word_of_int (uint a - uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1666
  by transfer (simp add: take_bit_diff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1667
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1668
lemma word_mult_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1669
  "a * b = word_of_int (uint a * uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1670
  by transfer (simp add: take_bit_eq_mod mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1671
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1672
lemma word_minus_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1673
  "- a = word_of_int (- uint a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1674
  by transfer (simp add: take_bit_minus)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1675
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1676
lemma word_0_wi:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1677
  "0 = word_of_int 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1678
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1679
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1680
lemma word_1_wi:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1681
  "1 = word_of_int 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1682
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1683
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1684
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1685
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1686
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1687
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1688
  by (auto simp add: take_bit_eq_mod intro: mod_diff_cong)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1689
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1690
lemma word_succ_alt [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1691
  "word_succ a = word_of_int (uint a + 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1692
  by transfer (simp add: take_bit_eq_mod mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1693
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1694
lemma word_pred_alt [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1695
  "word_pred a = word_of_int (uint a - 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1696
  by transfer (simp add: take_bit_eq_mod mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1697
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1698
lemmas word_arith_wis = 
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1699
  word_add_def word_sub_wi word_mult_def
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1700
  word_minus_def word_succ_alt word_pred_alt
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1701
  word_0_wi word_1_wi
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1702
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1703
lemma wi_homs:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1704
  shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1705
    and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1706
    and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1707
    and wi_hom_neg: "- word_of_int a = word_of_int (- a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1708
    and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1709
    and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1710
  by (transfer, simp)+
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1711
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1712
lemmas wi_hom_syms = wi_homs [symmetric]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1713
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1714
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1715
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1716
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1717
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1718
lemma double_eq_zero_iff:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1719
  \<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1720
  for a :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1721
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1722
  define n where \<open>n = LENGTH('a) - Suc 0\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1723
  then have *: \<open>LENGTH('a) = Suc n\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1724
    by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1725
  have \<open>a = 0\<close> if \<open>2 * a = 0\<close> and \<open>a \<noteq> 2 ^ (LENGTH('a) - Suc 0)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1726
    using that by transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1727
      (auto simp add: take_bit_eq_0_iff take_bit_eq_mod *)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1728
  moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1729
    by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1730
  then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1731
    by (simp add: *)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1732
  ultimately show ?thesis
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1733
    by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1734
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1735
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1736
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1737
subsection \<open>Ordering\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1738
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1739
lift_definition word_sle :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1740
  is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k \<le> signed_take_bit (LENGTH('a) - Suc 0) l\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1741
  by (simp flip: signed_take_bit_decr_length_iff)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1742
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1743
lift_definition word_sless :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1744
  is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - Suc 0) k < signed_take_bit (LENGTH('a) - Suc 0) l\<close>
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1745
  by (simp flip: signed_take_bit_decr_length_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1746
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1747
notation
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1748
  word_sle    ("'(\<le>s')") and
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1749
  word_sle    ("(_/ \<le>s _)"  [51, 51] 50) and
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1750
  word_sless  ("'(<s')") and
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1751
  word_sless  ("(_/ <s _)"  [51, 51] 50)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1752
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1753
notation (input)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1754
  word_sle    ("(_/ <=s _)"  [51, 51] 50)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1755
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1756
lemma word_sle_eq [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1757
  \<open>a <=s b \<longleftrightarrow> sint a \<le> sint b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1758
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1759
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1760
lemma [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1761
  \<open>a <s b \<longleftrightarrow> sint a < sint b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1762
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1763
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1764
lemma signed_ordering: \<open>ordering word_sle word_sless\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1765
  apply (standard; 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
  1766
  using signed_take_bit_decr_length_iff by force+
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1767
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1768
lemma signed_linorder: \<open>class.linorder word_sle word_sless\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1769
  by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1770
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1771
interpretation signed: linorder word_sle word_sless
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1772
  by (fact signed_linorder)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1773
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1774
lemma word_sless_eq:
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1775
  \<open>x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1776
  by (fact signed.less_le)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1777
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1778
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1779
  by (fact word_less_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1780
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1781
lemma word_zero_le [simp]: "0 \<le> y"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1782
  for y :: "'a::len word"
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1783
  by (fact word_coorder.extremum)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1784
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1785
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *)
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  1786
  by transfer (simp add: mask_eq_exp_minus_1)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1787
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1788
lemma word_n1_ge [simp]: "y \<le> -1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1789
  for y :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1790
  by (fact word_order.extremum)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1791
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1792
lemmas word_not_simps [simp] =
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1793
  word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1794
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1795
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1796
  for y :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1797
  by (simp add: less_le)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1798
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1799
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1800
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1801
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1802
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1803
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1804
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1805
  by transfer (simp add: nat_le_eq_zle)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1806
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1807
lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1808
  by transfer (auto simp add: less_le [of 0])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1809
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1810
lemmas unat_mono = word_less_nat_alt [THEN iffD1]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1811
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1812
instance word :: (len) wellorder
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1813
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1814
  fix P :: "'a word \<Rightarrow> bool" and a
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1815
  assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1816
  have "wf (measure unat)" ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1817
  moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1818
    by (auto simp add: word_less_nat_alt)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1819
  ultimately have "wf {(a, b :: ('a::len) word). a < b}"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1820
    by (rule wf_subset)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1821
  then show "P a" using *
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1822
    by induction blast
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1823
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1824
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1825
lemma wi_less:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1826
  "(word_of_int n < (word_of_int m :: 'a::len word)) =
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1827
    (n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1828
  by transfer (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1829
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1830
lemma wi_le:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1831
  "(word_of_int n \<le> (word_of_int m :: 'a::len word)) =
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1832
    (n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1833
  by transfer (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1834
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1835
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1836
subsection \<open>Bit-wise operations\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1837
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1838
context
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1839
  includes bit_operations_syntax
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1840
begin
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  1841
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1842
lemma uint_take_bit_eq:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1843
  \<open>uint (take_bit n w) = take_bit n (uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1844
  by transfer (simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1845
72227
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1846
lemma take_bit_word_eq_self:
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1847
  \<open>take_bit n w = w\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1848
  using that by transfer simp
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1849
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  1850
lemma take_bit_length_eq [simp]:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  1851
  \<open>take_bit LENGTH('a) w = w\<close> for w :: \<open>'a::len word\<close>
72227
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  1852
  by (rule take_bit_word_eq_self) simp
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  1853
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1854
lemma bit_word_of_int_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1855
  \<open>bit (word_of_int k :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> bit k n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1856
  by transfer rule
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1857
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1858
lemma bit_uint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1859
  \<open>bit (uint w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1860
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1861
  by transfer (simp add: bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1862
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1863
lemma bit_sint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1864
  \<open>bit (sint w) n \<longleftrightarrow> n \<ge> LENGTH('a) \<and> bit w (LENGTH('a) - 1) \<or> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1865
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1866
  by transfer (auto simp add: bit_signed_take_bit_iff min_def le_less not_less)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1867
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1868
lemma bit_word_ucast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1869
  \<open>bit (ucast w :: 'b::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> n < LENGTH('b) \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1870
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1871
  by transfer (simp add: bit_take_bit_iff ac_simps)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1872
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1873
lemma bit_word_scast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1874
  \<open>bit (scast w :: 'b::len word) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1875
    n < LENGTH('b) \<and> (bit w n \<or> LENGTH('a) \<le> n \<and> bit w (LENGTH('a) - Suc 0))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1876
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1877
  by transfer (auto simp add: bit_signed_take_bit_iff le_less min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1878
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1879
lemma bit_word_iff_drop_bit_and [code]:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1880
  \<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1881
  by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1882
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1883
lemma
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1884
  word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1885
    and word_and_def: "(a::'a word) AND b = word_of_int (uint a AND uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1886
    and word_or_def: "(a::'a word) OR b = word_of_int (uint a OR uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1887
    and word_xor_def: "(a::'a word) XOR b = word_of_int (uint a XOR uint b)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1888
  by (transfer, simp add: take_bit_not_take_bit)+
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  1889
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1890
definition even_word :: \<open>'a::len word \<Rightarrow> bool\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1891
  where [code_abbrev]: \<open>even_word = even\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1892
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1893
lemma even_word_iff [code]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1894
  \<open>even_word a \<longleftrightarrow> a AND 1 = 0\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1895
  by (simp add: and_one_eq even_iff_mod_2_eq_zero even_word_def)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1896
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1897
lemma map_bit_range_eq_if_take_bit_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1898
  \<open>map (bit k) [0..<n] = map (bit l) [0..<n]\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1899
  if \<open>take_bit n k = take_bit n l\<close> for k l :: int
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1900
using that proof (induction n arbitrary: k l)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1901
  case 0
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1902
  then show ?case
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1903
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1904
next
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1905
  case (Suc n)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1906
  from Suc.prems have \<open>take_bit n (k div 2) = take_bit n (l div 2)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1907
    by (simp add: take_bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1908
  then have \<open>map (bit (k div 2)) [0..<n] = map (bit (l div 2)) [0..<n]\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1909
    by (rule Suc.IH)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1910
  moreover have \<open>bit (r div 2) = bit r \<circ> Suc\<close> for r :: int
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1911
    by (simp add: fun_eq_iff bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1912
  moreover from Suc.prems have \<open>even k \<longleftrightarrow> even l\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1913
    by (auto simp add: take_bit_Suc elim!: evenE oddE) arith+
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1914
  ultimately show ?case
75085
ccc3a72210e6 Avoid overaggresive simplification.
haftmann
parents: 74592
diff changeset
  1915
    by (simp only: map_Suc_upt upt_conv_Cons flip: list.map_comp) (simp add: bit_0)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1916
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1917
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1918
lemma
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1919
  take_bit_word_Bit0_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit0 m) :: 'a::len word)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1920
    = 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?P)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1921
  and take_bit_word_Bit1_eq [simp]: \<open>take_bit (numeral n) (numeral (num.Bit1 m) :: 'a::len word)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1922
    = 1 + 2 * take_bit (pred_numeral n) (numeral m)\<close> (is ?Q)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1923
  and take_bit_word_minus_Bit0_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit0 m) :: 'a::len word)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1924
    = 2 * take_bit (pred_numeral n) (- numeral m)\<close> (is ?R)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1925
  and take_bit_word_minus_Bit1_eq [simp]: \<open>take_bit (numeral n) (- numeral (num.Bit1 m) :: 'a::len word)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1926
    = 1 + 2 * take_bit (pred_numeral n) (- numeral (Num.inc m))\<close> (is ?S)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1927
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1928
  define w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1929
    where \<open>w = numeral m\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1930
  moreover define q :: nat
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1931
    where \<open>q = pred_numeral n\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1932
  ultimately have num:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1933
    \<open>numeral m = w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1934
    \<open>numeral (num.Bit0 m) = 2 * w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1935
    \<open>numeral (num.Bit1 m) = 1 + 2 * w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1936
    \<open>numeral (Num.inc m) = 1 + w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1937
    \<open>pred_numeral n = q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1938
    \<open>numeral n = Suc q\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1939
    by (simp_all only: w_def q_def numeral_Bit0 [of m] numeral_Bit1 [of m] ac_simps
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1940
      numeral_inc numeral_eq_Suc flip: mult_2)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1941
  have even: \<open>take_bit (Suc q) (2 * w) = 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1942
    by (rule bit_word_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1943
      (auto simp add: bit_take_bit_iff bit_double_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1944
  have odd: \<open>take_bit (Suc q) (1 + 2 * w) = 1 + 2 * take_bit q w\<close> for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1945
    by (rule bit_eqI)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1946
      (auto simp add: bit_take_bit_iff bit_double_iff even_bit_succ_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1947
  show ?P
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1948
    using even [of w] by (simp add: num)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1949
  show ?Q
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1950
    using odd [of w] by (simp add: num)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1951
  show ?R
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1952
    using even [of \<open>- w\<close>] by (simp add: num)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1953
  show ?S
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1954
    using odd [of \<open>- (1 + w)\<close>] by (simp add: num)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1955
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  1956
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1957
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1958
subsection \<open>More shift operations\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1959
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1960
lift_definition signed_drop_bit :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a::len word\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1961
  is \<open>\<lambda>n. drop_bit n \<circ> signed_take_bit (LENGTH('a) - Suc 0)\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1962
  using signed_take_bit_decr_length_iff
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1963
  by (simp add: take_bit_drop_bit) force
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1964
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  1965
lemma bit_signed_drop_bit_iff [bit_simps]:
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1966
  \<open>bit (signed_drop_bit m w) n \<longleftrightarrow> bit w (if LENGTH('a) - m \<le> n \<and> n < LENGTH('a) then LENGTH('a) - 1 else m + n)\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1967
  for w :: \<open>'a::len word\<close>
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1968
  apply transfer
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1969
  apply (auto simp add: bit_drop_bit_eq bit_signed_take_bit_iff not_le min_def)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1970
   apply (metis add.commute le_antisym less_diff_conv less_eq_decr_length_iff)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1971
  apply (metis le_antisym less_eq_decr_length_iff)
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1972
  done
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  1973
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  1974
lemma [code]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  1975
  \<open>Word.the_int (signed_drop_bit n w) = take_bit LENGTH('a) (drop_bit n (Word.the_signed_int w))\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  1976
  for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  1977
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  1978
73816
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1979
lemma signed_drop_bit_of_0 [simp]:
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1980
  \<open>signed_drop_bit n 0 = 0\<close>
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1981
  by transfer simp
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1982
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1983
lemma signed_drop_bit_of_minus_1 [simp]:
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1984
  \<open>signed_drop_bit n (- 1) = - 1\<close>
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1985
  by transfer simp
0510c7a4256a moved more legacy to AFP
haftmann
parents: 73789
diff changeset
  1986
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  1987
lemma signed_drop_bit_signed_drop_bit [simp]:
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  1988
  \<open>signed_drop_bit m (signed_drop_bit n w) = signed_drop_bit (m + n) w\<close>
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  1989
  for w :: \<open>'a::len word\<close>
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
  1990
proof (cases \<open>LENGTH('a)\<close>)
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
  1991
  case 0
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
  1992
  then show ?thesis
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
  1993
    using len_not_eq_0 by blast
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
  1994
next
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
  1995
  case (Suc 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
  1996
  then show ?thesis
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
  1997
    by (force simp add: bit_signed_drop_bit_iff not_le less_diff_conv ac_simps intro!: bit_word_eqI)
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
  1998
qed
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  1999
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  2000
lemma signed_drop_bit_0 [simp]:
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  2001
  \<open>signed_drop_bit 0 w = w\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2002
  by transfer (simp add: take_bit_signed_take_bit)
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  2003
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  2004
lemma sint_signed_drop_bit_eq:
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  2005
  \<open>sint (signed_drop_bit n w) = drop_bit n (sint w)\<close>
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
  2006
proof (cases \<open>LENGTH('a) = 0 \<or> n=0\<close>)
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
  2007
  case False
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
  2008
  then show ?thesis
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
  2009
    apply simp
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
  2010
    apply (rule bit_eqI)
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
  2011
    by (auto simp add: bit_sint_iff bit_drop_bit_eq bit_signed_drop_bit_iff dest: bit_imp_le_length)
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
  2012
qed auto
72388
633d14bd1e59 consolidated for the sake of documentation
haftmann
parents: 72292
diff changeset
  2013
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2014
75623
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2015
subsection \<open>Single-bit operations\<close>
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2016
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2017
lemma set_bit_eq_idem_iff:
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2018
  \<open>Bit_Operations.set_bit n w = w \<longleftrightarrow> bit w n \<or> n \<ge> LENGTH('a)\<close>
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2019
  for w :: \<open>'a::len word\<close>
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2020
  by (simp add: bit_eq_iff) (auto simp add: bit_simps not_le)
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2021
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2022
lemma unset_bit_eq_idem_iff:
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2023
  \<open>unset_bit n w = w \<longleftrightarrow> bit w n \<longrightarrow> n \<ge> LENGTH('a)\<close>
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2024
  for w :: \<open>'a::len word\<close>
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2025
  by (simp add: bit_eq_iff) (auto simp add: bit_simps dest: bit_imp_le_length)
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2026
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2027
lemma flip_bit_eq_idem_iff:
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2028
  \<open>flip_bit n w = w \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2029
  for w :: \<open>'a::len word\<close>
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2030
  using linorder_le_less_linear
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2031
  by (simp add: bit_eq_iff) (auto simp add: bit_simps)
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2032
7a6301d01199 More lemmas.
haftmann
parents: 75087
diff changeset
  2033
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2034
subsection \<open>Rotation\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2035
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2036
lift_definition word_rotr :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2037
  is \<open>\<lambda>n k. concat_bit (LENGTH('a) - n mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2038
    (drop_bit (n mod LENGTH('a)) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2039
    (take_bit (n mod LENGTH('a)) k)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2040
  subgoal for n k l
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
  2041
    by (simp add: concat_bit_def nat_le_iff less_imp_le
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2042
      take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>n mod LENGTH('a::len)\<close>])
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2043
  done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2044
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2045
lift_definition word_rotl :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2046
  is \<open>\<lambda>n k. concat_bit (n mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2047
    (drop_bit (LENGTH('a) - n mod LENGTH('a)) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2048
    (take_bit (LENGTH('a) - n mod LENGTH('a)) k)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2049
  subgoal for n k l
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
  2050
    by (simp add: concat_bit_def nat_le_iff less_imp_le
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2051
      take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>LENGTH('a) - n mod LENGTH('a::len)\<close>])
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2052
  done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2053
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2054
lift_definition word_roti :: \<open>int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2055
  is \<open>\<lambda>r k. concat_bit (LENGTH('a) - nat (r mod int LENGTH('a)))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2056
    (drop_bit (nat (r mod int LENGTH('a))) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2057
    (take_bit (nat (r mod int LENGTH('a))) k)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2058
  subgoal for r k l
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
  2059
    by (simp add: concat_bit_def nat_le_iff less_imp_le
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2060
      take_bit_tightened [of \<open>LENGTH('a)\<close> k l \<open>nat (r mod int LENGTH('a::len))\<close>])
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2061
  done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2062
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2063
lemma word_rotl_eq_word_rotr [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2064
  \<open>word_rotl n = (word_rotr (LENGTH('a) - n mod LENGTH('a)) :: 'a::len word \<Rightarrow> 'a word)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2065
  by (rule ext, cases \<open>n mod LENGTH('a) = 0\<close>; transfer) simp_all
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2066
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2067
lemma word_roti_eq_word_rotr_word_rotl [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2068
  \<open>word_roti i w =
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2069
    (if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat (- i)) w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2070
proof (cases \<open>i \<ge> 0\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2071
  case True
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2072
  moreover define n where \<open>n = nat i\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2073
  ultimately have \<open>i = int n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2074
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2075
  moreover have \<open>word_roti (int n) = (word_rotr n :: _ \<Rightarrow> 'a word)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2076
    by (rule ext, transfer) (simp add: nat_mod_distrib)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2077
  ultimately show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2078
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2079
next
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2080
  case False
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2081
  moreover define n where \<open>n = nat (- i)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2082
  ultimately have \<open>i = - int n\<close> \<open>n > 0\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2083
    by simp_all
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2084
  moreover have \<open>word_roti (- int n) = (word_rotl n :: _ \<Rightarrow> 'a word)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2085
    by (rule ext, transfer)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2086
      (simp add: zmod_zminus1_eq_if flip: of_nat_mod of_nat_diff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2087
  ultimately show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2088
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2089
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2090
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  2091
lemma bit_word_rotr_iff [bit_simps]:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2092
  \<open>bit (word_rotr m w) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2093
    n < LENGTH('a) \<and> bit w ((n + m) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2094
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2095
proof transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2096
  fix k :: int and m n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2097
  define q where \<open>q = m mod LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2098
  have \<open>q < LENGTH('a)\<close> 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2099
    by (simp add: q_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2100
  then have \<open>q \<le> LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2101
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2102
  have \<open>m mod LENGTH('a) = q\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2103
    by (simp add: q_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2104
  moreover have \<open>(n + m) mod LENGTH('a) = (n + q) mod LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2105
    by (subst mod_add_right_eq [symmetric]) (simp add: \<open>m mod LENGTH('a) = q\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2106
  moreover have \<open>n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2107
    bit (concat_bit (LENGTH('a) - q) (drop_bit q (take_bit LENGTH('a) k)) (take_bit q k)) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2108
    n < LENGTH('a) \<and> bit k ((n + q) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2109
    using \<open>q < LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2110
    by (cases \<open>q + n \<ge> LENGTH('a)\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2111
     (auto simp add: bit_concat_bit_iff bit_drop_bit_eq
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2112
        bit_take_bit_iff le_mod_geq ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2113
  ultimately show \<open>n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2114
    bit (concat_bit (LENGTH('a) - m mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2115
      (drop_bit (m mod LENGTH('a)) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2116
      (take_bit (m mod LENGTH('a)) k)) n
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2117
    \<longleftrightarrow> n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2118
      (n + m) mod LENGTH('a) < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2119
      bit k ((n + m) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2120
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2121
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2122
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  2123
lemma bit_word_rotl_iff [bit_simps]:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2124
  \<open>bit (word_rotl m w) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2125
    n < LENGTH('a) \<and> bit w ((n + (LENGTH('a) - m mod LENGTH('a))) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2126
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2127
  by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2128
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  2129
lemma bit_word_roti_iff [bit_simps]:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2130
  \<open>bit (word_roti k w) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2131
    n < LENGTH('a) \<and> bit w (nat ((int n + k) mod int LENGTH('a)))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2132
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2133
proof transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2134
  fix k l :: int and n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2135
  define m where \<open>m = nat (k mod int LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2136
  have \<open>m < LENGTH('a)\<close> 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2137
    by (simp add: nat_less_iff m_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2138
  then have \<open>m \<le> LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2139
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2140
  have \<open>k mod int LENGTH('a) = int m\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2141
    by (simp add: nat_less_iff m_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2142
  moreover have \<open>(int n + k) mod int LENGTH('a) = int ((n + m) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2143
    by (subst mod_add_right_eq [symmetric]) (simp add: of_nat_mod \<open>k mod int LENGTH('a) = int m\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2144
  moreover have \<open>n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2145
    bit (concat_bit (LENGTH('a) - m) (drop_bit m (take_bit LENGTH('a) l)) (take_bit m l)) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2146
    n < LENGTH('a) \<and> bit l ((n + m) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2147
    using \<open>m < LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2148
    by (cases \<open>m + n \<ge> LENGTH('a)\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2149
     (auto simp add: bit_concat_bit_iff bit_drop_bit_eq
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2150
        bit_take_bit_iff nat_less_iff not_le not_less ac_simps
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2151
        le_diff_conv le_mod_geq)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2152
  ultimately show \<open>n < LENGTH('a)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2153
    \<and> bit (concat_bit (LENGTH('a) - nat (k mod int LENGTH('a)))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2154
             (drop_bit (nat (k mod int LENGTH('a))) (take_bit LENGTH('a) l))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2155
             (take_bit (nat (k mod int LENGTH('a))) l)) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2156
       n < LENGTH('a) 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2157
    \<and> nat ((int n + k) mod int LENGTH('a)) < LENGTH('a)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2158
    \<and> bit l (nat ((int n + k) mod int LENGTH('a)))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2159
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2160
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2161
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2162
lemma uint_word_rotr_eq:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2163
  \<open>uint (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2164
    (drop_bit (n mod LENGTH('a)) (uint w))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2165
    (uint (take_bit (n mod LENGTH('a)) w))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2166
  for w :: \<open>'a::len word\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  2167
  by transfer (simp add: take_bit_concat_bit_eq)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2168
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2169
lemma [code]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2170
  \<open>Word.the_int (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a))
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2171
    (drop_bit (n mod LENGTH('a)) (Word.the_int w))
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2172
    (Word.the_int (take_bit (n mod LENGTH('a)) w))\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2173
  for w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2174
  using uint_word_rotr_eq [of n w] by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2175
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2176
    
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2177
subsection \<open>Split and cat operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2178
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2179
lift_definition word_cat :: \<open>'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2180
  is \<open>\<lambda>k l. concat_bit LENGTH('b) l (take_bit LENGTH('a) k)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2181
  by (simp add: bit_eq_iff bit_concat_bit_iff bit_take_bit_iff)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2182
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  2183
lemma word_cat_eq:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  2184
  \<open>(word_cat v w :: 'c::len word) = push_bit LENGTH('b) (ucast v) + ucast w\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  2185
  for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2186
  by transfer (simp add: concat_bit_eq ac_simps)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2187
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2188
lemma word_cat_eq' [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2189
  \<open>word_cat a b = word_of_int (concat_bit LENGTH('b) (uint b) (uint a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2190
  for a :: \<open>'a::len word\<close> and b :: \<open>'b::len word\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2191
  by transfer (simp add: concat_bit_take_bit_eq)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  2192
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  2193
lemma bit_word_cat_iff [bit_simps]:
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  2194
  \<open>bit (word_cat v w :: 'c::len word) n \<longleftrightarrow> n < LENGTH('c) \<and> (if n < LENGTH('b) then bit w n else bit v (n - LENGTH('b)))\<close> 
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  2195
  for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2196
  by transfer (simp add: bit_concat_bit_iff bit_take_bit_iff)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  2197
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2198
definition word_split :: \<open>'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word\<close>
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2199
  where \<open>word_split w =
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2200
    (ucast (drop_bit LENGTH('c) w) :: 'b::len word, ucast w :: 'c::len word)\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2201
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  2202
definition word_rcat :: \<open>'a::len word list \<Rightarrow> 'b::len word\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  2203
  where \<open>word_rcat = word_of_int \<circ> horner_sum uint (2 ^ LENGTH('a)) \<circ> rev\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  2204
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2205
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2206
subsection \<open>More on conversions\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2207
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2208
lemma int_word_sint:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2209
  \<open>sint (word_of_int x :: 'a::len word) = (x + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2210
  by transfer (simp flip: take_bit_eq_mod add: signed_take_bit_eq_take_bit_shift)
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  2211
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2212
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) bin"
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  2213
  by (simp add: signed_of_int)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2214
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2215
lemma uint_sint: "uint w = take_bit LENGTH('a) (sint w)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2216
  for w :: "'a::len word"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2217
  by transfer (simp add: take_bit_signed_take_bit)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2218
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2219
lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> take_bit n (uint w) = uint w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2220
  for w :: "'a::len word"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2221
  by transfer (simp add: min_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2222
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  2223
lemma wi_bintr:
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
  2224
  "LENGTH('a::len) \<le> n \<Longrightarrow>
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2225
    word_of_int (take_bit n w) = (word_of_int w :: 'a word)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2226
  by transfer simp
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2227
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2228
lemma word_numeral_alt: "numeral b = word_of_int (numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2229
  by (induct b, simp_all only: numeral.simps word_of_int_homs)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2230
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2231
declare word_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2232
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2233
lemma word_neg_numeral_alt: "- numeral b = word_of_int (- numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2234
  by (simp only: word_numeral_alt wi_hom_neg)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2235
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2236
declare word_neg_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2237
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2238
lemma uint_bintrunc [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2239
  "uint (numeral bin :: 'a word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2240
    take_bit (LENGTH('a::len)) (numeral bin)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2241
  by transfer rule
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2242
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2243
lemma uint_bintrunc_neg [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2244
  "uint (- numeral bin :: 'a word) = take_bit (LENGTH('a::len)) (- numeral bin)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2245
  by transfer rule
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2246
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2247
lemma sint_sbintrunc [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2248
  "sint (numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (numeral bin)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2249
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2250
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2251
lemma sint_sbintrunc_neg [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2252
  "sint (- numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (- numeral bin)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2253
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2254
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2255
lemma unat_bintrunc [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2256
  "unat (numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (numeral bin))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2257
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2258
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2259
lemma unat_bintrunc_neg [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2260
  "unat (- numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (- numeral bin))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2261
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2262
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2263
lemma size_0_eq: "size w = 0 \<Longrightarrow> v = w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2264
  for v w :: "'a::len word"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2265
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2266
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2267
lemma uint_ge_0 [iff]: "0 \<le> uint x"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2268
  by (fact unsigned_greater_eq)
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2269
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2270
lemma uint_lt2p [iff]: "uint x < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2271
  for x :: "'a::len word"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2272
  by (fact unsigned_less)
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2273
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2274
lemma sint_ge: "- (2 ^ (LENGTH('a) - 1)) \<le> sint x"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2275
  for x :: "'a::len word"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2276
  using sint_greater_eq [of x] by simp
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2277
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2278
lemma sint_lt: "sint x < 2 ^ (LENGTH('a) - 1)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2279
  for x :: "'a::len word"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2280
  using sint_less [of x] by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2281
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2282
lemma uint_m2p_neg: "uint x - 2 ^ LENGTH('a) < 0"
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
  2283
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2284
  by (simp only: diff_less_0_iff_less uint_lt2p)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2285
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2286
lemma uint_m2p_not_non_neg: "\<not> 0 \<le> uint x - 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2287
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2288
  by (simp only: not_le uint_m2p_neg)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2289
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2290
lemma lt2p_lem: "LENGTH('a) \<le> n \<Longrightarrow> uint w < 2 ^ n"
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
  2291
  for w :: "'a::len word"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2292
  using uint_bounded [of w] by (rule less_le_trans) simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2293
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2294
lemma uint_le_0_iff [simp]: "uint x \<le> 0 \<longleftrightarrow> uint x = 0"
70749
5d06b7bb9d22 More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents: 70342
diff changeset
  2295
  by (fact uint_ge_0 [THEN leD, THEN antisym_conv1])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2296
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  2297
lemma uint_nat: "uint w = int (unat w)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2298
  by transfer simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2299
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
  2300
lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2301
  by (simp flip: take_bit_eq_mod add: of_nat_take_bit)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2302
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
  2303
lemma uint_neg_numeral: "uint (- numeral b :: 'a::len word) = - numeral b mod 2 ^ LENGTH('a)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2304
  by (simp flip: take_bit_eq_mod add: of_nat_take_bit)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2305
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
  2306
lemma unat_numeral: "unat (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2307
  by transfer (simp add: take_bit_eq_mod nat_mod_distrib nat_power_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2308
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2309
lemma sint_numeral:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2310
  "sint (numeral b :: 'a::len word) =
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2311
    (numeral b + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) - 2 ^ (LENGTH('a) - 1)"
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
  2312
  by (metis int_word_sint word_numeral_alt)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2313
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2314
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2315
  by (fact of_int_0)
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  2316
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2317
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2318
  by (fact of_int_1)
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  2319
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2320
lemma word_of_int_neg_1 [simp]: "word_of_int (- 1) = - 1"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2321
  by (simp add: wi_hom_syms)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2322
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
  2323
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2324
  by (fact of_int_numeral)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2325
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2326
lemma word_of_int_neg_numeral [simp]:
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
  2327
  "(word_of_int (- numeral bin) :: 'a::len word) = - numeral bin"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2328
  by (fact of_int_neg_numeral)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2329
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2330
lemma word_int_case_wi:
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
  2331
  "word_int_case f (word_of_int i :: 'b word) = f (i mod 2 ^ LENGTH('b::len))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2332
  by transfer (simp add: take_bit_eq_mod)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2333
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2334
lemma word_int_split:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2335
  "P (word_int_case f x) =
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
  2336
    (\<forall>i. x = (word_of_int i :: 'b::len word) \<and> 0 \<le> i \<and> i < 2 ^ LENGTH('b) \<longrightarrow> P (f i))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2337
  by transfer (auto simp add: take_bit_eq_mod)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2338
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2339
lemma word_int_split_asm:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2340
  "P (word_int_case f x) =
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
  2341
    (\<nexists>n. x = (word_of_int n :: 'b::len word) \<and> 0 \<le> n \<and> n < 2 ^ LENGTH('b::len) \<and> \<not> P (f n))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2342
  by transfer (auto simp add: take_bit_eq_mod)
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2343
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2344
lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2345
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2346
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2347
lemma sint_range_size: "- (2 ^ (size w - Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w - Suc 0)"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2348
  by (simp add: word_size sint_greater_eq sint_less)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2349
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2350
lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2351
  for w :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2352
  unfolding word_size by (rule less_le_trans [OF sint_lt])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2353
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2354
lemma sint_below_size: "x \<le> - (2 ^ (size w - 1)) \<Longrightarrow> x \<le> sint w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2355
  for w :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2356
  unfolding word_size by (rule order_trans [OF _ sint_ge])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2357
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2358
lemma word_unat_eq_iff:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2359
  \<open>v = w \<longleftrightarrow> unat v = unat w\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2360
  for v w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2361
  by (fact word_eq_iff_unsigned)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2362
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2363
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2364
subsection \<open>Testing bits\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  2365
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2366
lemma bin_nth_uint_imp: "bit (uint w) n \<Longrightarrow> n < LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2367
  for w :: "'a::len word"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2368
  by transfer (simp add: bit_take_bit_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2369
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  2370
lemma bin_nth_sint:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2371
  "LENGTH('a) \<le> n \<Longrightarrow>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2372
    bit (sint w) n = bit (sint w) (LENGTH('a) - 1)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2373
  for w :: "'a::len word"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2374
  by (transfer fixing: n) (simp add: bit_signed_take_bit_iff le_diff_conv min_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2375
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2376
lemma num_of_bintr':
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2377
  "take_bit (LENGTH('a::len)) (numeral a :: int) = (numeral b) \<Longrightarrow>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2378
    numeral a = (numeral b :: 'a word)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2379
proof (transfer fixing: a b)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2380
  assume \<open>take_bit LENGTH('a) (numeral a :: int) = numeral b\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2381
  then have \<open>take_bit LENGTH('a) (take_bit LENGTH('a) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2382
    by simp
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2383
  then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2384
    by simp
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2385
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2386
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2387
lemma num_of_sbintr':
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  2388
  "signed_take_bit (LENGTH('a::len) - 1) (numeral a :: int) = (numeral b) \<Longrightarrow>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2389
    numeral a = (numeral b :: 'a word)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2390
proof (transfer fixing: a b)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2391
  assume \<open>signed_take_bit (LENGTH('a) - 1) (numeral a :: int) = numeral b\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2392
  then have \<open>take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - 1) (numeral a :: int)) = take_bit LENGTH('a) (numeral b)\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2393
    by simp
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2394
  then show \<open>take_bit LENGTH('a) (numeral a :: int) = take_bit LENGTH('a) (numeral b)\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2395
    by (simp add: take_bit_signed_take_bit)
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2396
qed
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2397
 
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  2398
lemma num_abs_bintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2399
  "(numeral x :: 'a word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2400
    word_of_int (take_bit (LENGTH('a::len)) (numeral x))"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2401
  by transfer simp
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  2402
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  2403
lemma num_abs_sbintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2404
  "(numeral x :: 'a word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2405
    word_of_int (signed_take_bit (LENGTH('a::len) - 1) (numeral x))"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2406
  by transfer (simp add: take_bit_signed_take_bit)
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  2407
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2408
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2409
  \<open>cast\<close> -- note, no arg for new length, as it's determined by type of result,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2410
  thus in \<open>cast w = w\<close>, the type means cast to length of \<open>w\<close>!
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2411
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2412
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2413
lemma bit_ucast_iff:
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  2414
  \<open>bit (ucast a :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a::len) \<and> bit a n\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2415
  by transfer (simp add: bit_take_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2416
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2417
lemma ucast_id [simp]: "ucast w = w"
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2418
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2419
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2420
lemma scast_id [simp]: "scast w = w"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2421
  by transfer (simp add: take_bit_signed_take_bit)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2422
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2423
lemma ucast_mask_eq:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  2424
  \<open>ucast (mask n :: 'b word) = mask (min LENGTH('b::len) n)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  2425
  by (simp add: bit_eq_iff) (auto simp add: bit_mask_iff bit_ucast_iff)
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2426
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2427
\<comment> \<open>literal u(s)cast\<close>
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  2428
lemma ucast_bintr [simp]:
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
  2429
  "ucast (numeral w :: 'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2430
    word_of_int (take_bit (LENGTH('a)) (numeral w))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2431
  by transfer simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2432
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2433
(* TODO: neg_numeral *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2434
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  2435
lemma scast_sbintr [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2436
  "scast (numeral w ::'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2437
    word_of_int (signed_take_bit (LENGTH('a) - Suc 0) (numeral w))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2438
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2439
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
  2440
lemma source_size: "source_size (c::'a::len word \<Rightarrow> _) = LENGTH('a)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2441
  by transfer simp
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2442
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
  2443
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len word) = LENGTH('b)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2444
  by transfer simp
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2445
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2446
lemma is_down: "is_down c \<longleftrightarrow> LENGTH('b) \<le> LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2447
  for c :: "'a::len word \<Rightarrow> 'b::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2448
  by transfer simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2449
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2450
lemma is_up: "is_up c \<longleftrightarrow> LENGTH('a) \<le> LENGTH('b)"
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
  2451
  for c :: "'a::len word \<Rightarrow> 'b::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2452
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2453
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2454
lemma is_up_down:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2455
  \<open>is_up c \<longleftrightarrow> is_down d\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2456
  for c :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2457
  and d :: \<open>'b::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2458
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2459
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2460
context
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2461
  fixes dummy_types :: \<open>'a::len \<times> 'b::len\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2462
begin
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2463
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2464
private abbreviation (input) UCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2465
  where \<open>UCAST == ucast\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2466
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2467
private abbreviation (input) SCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2468
  where \<open>SCAST == scast\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2469
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2470
lemma down_cast_same:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2471
  \<open>UCAST = scast\<close> if \<open>is_down UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2472
  by (rule ext, use that in transfer) (simp add: take_bit_signed_take_bit)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2473
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2474
lemma sint_up_scast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2475
  \<open>sint (SCAST w) = sint w\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2476
  using that by transfer (simp add: min_def Suc_leI le_diff_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2477
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2478
lemma uint_up_ucast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2479
  \<open>uint (UCAST w) = uint w\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2480
  using that by transfer (simp add: min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2481
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2482
lemma ucast_up_ucast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2483
  \<open>ucast (UCAST w) = ucast w\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2484
  using that by transfer (simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2485
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2486
lemma ucast_up_ucast_id:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2487
  \<open>ucast (UCAST w) = w\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2488
  using that by (simp add: ucast_up_ucast)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2489
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2490
lemma scast_up_scast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2491
  \<open>scast (SCAST w) = scast w\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2492
  using that by transfer (simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2493
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2494
lemma scast_up_scast_id:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2495
  \<open>scast (SCAST w) = w\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2496
  using that by (simp add: scast_up_scast)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2497
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2498
lemma isduu:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2499
  \<open>is_up UCAST\<close> if \<open>is_down d\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2500
    for d :: \<open>'b word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2501
  using that is_up_down [of UCAST d] by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2502
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2503
lemma isdus:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2504
  \<open>is_up SCAST\<close> if \<open>is_down d\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2505
    for d :: \<open>'b word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2506
  using that is_up_down [of SCAST d] by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2507
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2508
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2509
lemmas scast_down_scast_id = isdus [THEN scast_up_scast_id]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2510
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2511
lemma up_ucast_surj:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2512
  \<open>surj (ucast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2513
  by (rule surjI) (use that in \<open>rule ucast_up_ucast_id\<close>)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2514
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2515
lemma up_scast_surj:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2516
  \<open>surj (scast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2517
  by (rule surjI) (use that in \<open>rule scast_up_scast_id\<close>)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2518
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2519
lemma down_ucast_inj:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2520
  \<open>inj_on UCAST A\<close> if \<open>is_down (ucast :: 'b word \<Rightarrow> 'a word)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2521
  by (rule inj_on_inverseI) (use that in \<open>rule ucast_down_ucast_id\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2522
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2523
lemma down_scast_inj:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2524
  \<open>inj_on SCAST A\<close> if \<open>is_down (scast :: 'b word \<Rightarrow> 'a word)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2525
  by (rule inj_on_inverseI) (use that in \<open>rule scast_down_scast_id\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2526
  
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2527
lemma ucast_down_wi:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2528
  \<open>UCAST (word_of_int x) = word_of_int x\<close> if \<open>is_down UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2529
  using that by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2530
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2531
lemma ucast_down_no:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2532
  \<open>UCAST (numeral bin) = numeral bin\<close> if \<open>is_down UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2533
  using that by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2534
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2535
end
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2536
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2537
lemmas word_log_defs = word_and_def word_or_def word_xor_def word_not_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2538
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2539
lemma bit_last_iff:
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2540
  \<open>bit w (LENGTH('a) - Suc 0) \<longleftrightarrow> sint w < 0\<close> (is \<open>?P \<longleftrightarrow> ?Q\<close>)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2541
  for w :: \<open>'a::len word\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2542
proof -
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2543
  have \<open>?P \<longleftrightarrow> bit (uint w) (LENGTH('a) - Suc 0)\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2544
    by (simp add: bit_uint_iff)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2545
  also have \<open>\<dots> \<longleftrightarrow> ?Q\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  2546
    by (simp add: sint_uint)
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2547
  finally show ?thesis .
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2548
qed
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2549
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2550
lemma drop_bit_eq_zero_iff_not_bit_last:
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2551
  \<open>drop_bit (LENGTH('a) - Suc 0) w = 0 \<longleftrightarrow> \<not> bit w (LENGTH('a) - Suc 0)\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2552
  for w :: "'a::len word"
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
  2553
proof (cases \<open>LENGTH('a)\<close>)
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
  2554
  case (Suc 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
  2555
  then show ?thesis
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2556
    apply transfer
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2557
    apply (simp add: take_bit_drop_bit)
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  2558
    by (simp add: bit_iff_odd_drop_bit drop_bit_take_bit odd_iff_mod_2_eq_one)
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
  2559
qed auto
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2560
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2561
lemma unat_div:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2562
  \<open>unat (x div y) = unat x div unat y\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2563
  by (fact unat_div_distrib)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2564
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2565
lemma unat_mod:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2566
  \<open>unat (x mod y) = unat x mod unat y\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2567
  by (fact unat_mod_distrib)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2568
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2569
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2570
subsection \<open>Word Arithmetic\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2571
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2572
lemmas less_eq_word_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2573
  word_le_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2574
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2575
lemmas less_word_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2576
  word_less_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2577
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2578
lemmas less_eq_word_minus_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2579
  word_le_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2580
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2581
lemmas less_word_minus_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2582
  word_less_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2583
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2584
lemmas less_eq_word_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2585
  word_le_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2586
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2587
lemmas less_word_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2588
  word_less_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2589
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2590
lemmas less_eq_word_minus_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2591
  word_le_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2592
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2593
lemmas less_word_minus_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2594
  word_less_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2595
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2596
lemmas less_word_numeral_minus_1 [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2597
  word_less_def [of \<open>numeral a\<close> \<open>- 1\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2598
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2599
lemmas less_word_minus_numeral_minus_1 [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2600
  word_less_def [of \<open>- numeral a\<close> \<open>- 1\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2601
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2602
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2603
lemmas sless_eq_word_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2604
  word_sle_eq [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2605
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2606
lemmas sless_word_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2607
  word_sless_alt [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2608
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2609
lemmas sless_eq_word_minus_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2610
  word_sle_eq [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2611
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2612
lemmas sless_word_minus_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2613
  word_sless_alt [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2614
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2615
lemmas sless_eq_word_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2616
  word_sle_eq [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2617
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2618
lemmas sless_word_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2619
  word_sless_alt [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2620
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2621
lemmas sless_eq_word_minus_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2622
  word_sle_eq [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2623
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2624
lemmas sless_word_minus_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2625
  word_sless_alt [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified sint_sbintrunc sint_sbintrunc_neg]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2626
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2627
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2628
lemmas div_word_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2629
  word_div_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2630
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2631
lemmas div_word_minus_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2632
  word_div_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2633
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2634
lemmas div_word_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2635
  word_div_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2636
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2637
lemmas div_word_minus_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2638
  word_div_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2639
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2640
lemmas div_word_minus_1_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2641
  word_div_def [of \<open>- 1\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2642
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2643
lemmas div_word_minus_1_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2644
  word_div_def [of \<open>- 1\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2645
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2646
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2647
lemmas mod_word_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2648
  word_mod_def [of \<open>numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2649
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2650
lemmas mod_word_minus_numeral_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2651
  word_mod_def [of \<open>- numeral a\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2652
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2653
lemmas mod_word_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2654
  word_mod_def [of \<open>numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2655
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2656
lemmas mod_word_minus_numeral_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2657
  word_mod_def [of \<open>- numeral a\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2658
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2659
lemmas mod_word_minus_1_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2660
  word_mod_def [of \<open>- 1\<close> \<open>numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2661
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2662
lemmas mod_word_minus_1_minus_numeral [simp] =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2663
  word_mod_def [of \<open>- 1\<close> \<open>- numeral b\<close>, simplified uint_bintrunc uint_bintrunc_neg unsigned_minus_1_eq_mask mask_eq_exp_minus_1]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2664
  for a b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2665
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2666
lemma signed_drop_bit_of_1 [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2667
  \<open>signed_drop_bit n (1 :: 'a::len word) = of_bool (LENGTH('a) = 1 \<or> n = 0)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2668
  apply (transfer fixing: n)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2669
  apply (cases \<open>LENGTH('a)\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2670
   apply (auto simp add: take_bit_signed_take_bit)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2671
  apply (auto simp add: take_bit_drop_bit gr0_conv_Suc simp flip: take_bit_eq_self_iff_drop_bit_eq_0)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2672
  done
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2673
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2674
lemma take_bit_word_beyond_length_eq:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2675
  \<open>take_bit n w = w\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2676
  using that by transfer simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2677
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2678
lemmas word_div_no [simp] = word_div_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2679
lemmas word_mod_no [simp] = word_mod_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2680
lemmas word_less_no [simp] = word_less_def [of "numeral a" "numeral b"] for a b
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2681
lemmas word_le_no [simp] = word_le_def [of "numeral a" "numeral b"] for a b
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2682
lemmas word_sless_no [simp] = word_sless_eq [of "numeral a" "numeral b"] for a b
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2683
lemmas word_sle_no [simp] = word_sle_eq [of "numeral a" "numeral b"] for a b
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2684
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2685
lemma size_0_same': "size w = 0 \<Longrightarrow> w = v"
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
  2686
  for v w :: "'a::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2687
  by (unfold word_size) simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2688
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2689
lemmas size_0_same = size_0_same' [unfolded word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2690
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2691
lemmas unat_eq_0 = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2692
lemmas unat_eq_zero = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2693
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2694
lemma mask_1: "mask 1 = 1"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2695
  by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2696
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2697
lemma mask_Suc_0: "mask (Suc 0) = 1"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2698
  by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2699
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2700
lemma bin_last_bintrunc: "odd (take_bit l n) \<longleftrightarrow> l > 0 \<and> odd n"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2701
  by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2702
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2703
lemma push_bit_word_beyond [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2704
  \<open>push_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2705
  using that by (transfer fixing: n) (simp add: take_bit_push_bit)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2706
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2707
lemma drop_bit_word_beyond [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2708
  \<open>drop_bit n w = 0\<close> if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2709
  using that by (transfer fixing: n) (simp add: drop_bit_take_bit)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2710
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2711
lemma signed_drop_bit_beyond:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2712
  \<open>signed_drop_bit n w = (if bit w (LENGTH('a) - Suc 0) then - 1 else 0)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2713
  if \<open>LENGTH('a) \<le> n\<close> for w :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2714
  by (rule bit_word_eqI) (simp add: bit_signed_drop_bit_iff that)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2715
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2716
lemma take_bit_numeral_minus_numeral_word [simp]:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2717
  \<open>take_bit (numeral m) (- numeral n :: 'a::len word) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2718
    (case take_bit_num (numeral m) n of None \<Rightarrow> 0 | Some q \<Rightarrow> take_bit (numeral m) (2 ^ numeral m - numeral q))\<close> (is \<open>?lhs = ?rhs\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2719
proof (cases \<open>LENGTH('a) \<le> numeral m\<close>)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2720
  case True
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2721
  then have *: \<open>(take_bit (numeral m) :: 'a word \<Rightarrow> 'a word) = id\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2722
    by (simp add: fun_eq_iff take_bit_word_eq_self)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2723
  have **: \<open>2 ^ numeral m = (0 :: 'a word)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2724
    using True by (simp flip: exp_eq_zero_iff)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2725
  show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2726
    by (auto simp only: * ** split: option.split
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2727
      dest!: take_bit_num_eq_None_imp [where ?'a = \<open>'a word\<close>] take_bit_num_eq_Some_imp [where ?'a = \<open>'a word\<close>])
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2728
      simp_all
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2729
next
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2730
  case False
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2731
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2732
    by (transfer fixing: m n) simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2733
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2734
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2735
lemma of_nat_inverse:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2736
  \<open>word_of_nat r = a \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> unat a = r\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2737
  for a :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2738
  by (metis id_apply of_nat_eq_id take_bit_nat_eq_self_iff unsigned_of_nat)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  2739
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2740
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2741
subsection \<open>Transferring goals from words to ints\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2742
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2743
lemma word_ths:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2744
  shows word_succ_p1: "word_succ a = a + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2745
    and word_pred_m1: "word_pred a = a - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2746
    and word_pred_succ: "word_pred (word_succ a) = a"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2747
    and word_succ_pred: "word_succ (word_pred a) = a"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2748
    and word_mult_succ: "word_succ a * b = b + a * b"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  2749
  by (transfer, simp add: algebra_simps)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2750
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2751
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2752
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2753
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2754
lemma uint_word_ariths:
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
  2755
  fixes a b :: "'a::len word"
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
  2756
  shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len)"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2757
    and "uint (a - b) = (uint a - uint b) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2758
    and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2759
    and "uint (- a) = - uint a mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2760
    and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2761
    and "uint (word_pred a) = (uint a - 1) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2762
    and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2763
    and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2764
  by (simp_all only: word_arith_wis uint_word_of_int_eq flip: take_bit_eq_mod)
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2765
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2766
lemma uint_word_arith_bintrs:
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
  2767
  fixes a b :: "'a::len word"
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2768
  shows "uint (a + b) = take_bit (LENGTH('a)) (uint a + uint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2769
    and "uint (a - b) = take_bit (LENGTH('a)) (uint a - uint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2770
    and "uint (a * b) = take_bit (LENGTH('a)) (uint a * uint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2771
    and "uint (- a) = take_bit (LENGTH('a)) (- uint a)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2772
    and "uint (word_succ a) = take_bit (LENGTH('a)) (uint a + 1)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2773
    and "uint (word_pred a) = take_bit (LENGTH('a)) (uint a - 1)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2774
    and "uint (0 :: 'a word) = take_bit (LENGTH('a)) 0"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2775
    and "uint (1 :: 'a word) = take_bit (LENGTH('a)) 1"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2776
  by (simp_all add: uint_word_ariths take_bit_eq_mod)
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2777
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2778
lemma sint_word_ariths:
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2779
  fixes a b :: "'a::len word"
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2780
  shows "sint (a + b) = signed_take_bit (LENGTH('a) - 1) (sint a + sint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2781
    and "sint (a - b) = signed_take_bit (LENGTH('a) - 1) (sint a - sint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2782
    and "sint (a * b) = signed_take_bit (LENGTH('a) - 1) (sint a * sint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2783
    and "sint (- a) = signed_take_bit (LENGTH('a) - 1) (- sint a)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2784
    and "sint (word_succ a) = signed_take_bit (LENGTH('a) - 1) (sint a + 1)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2785
    and "sint (word_pred a) = signed_take_bit (LENGTH('a) - 1) (sint a - 1)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2786
    and "sint (0 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 0"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2787
    and "sint (1 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 1"
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
  2788
  subgoal
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
  2789
    by transfer (simp add: signed_take_bit_add)
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
  2790
  subgoal
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
  2791
    by transfer (simp add: signed_take_bit_diff)
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
  2792
  subgoal
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
  2793
    by transfer (simp add: signed_take_bit_mult)
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
  2794
  subgoal
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
  2795
    by transfer (simp add: signed_take_bit_minus)
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2796
     apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_succ)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2797
    apply (metis of_int_sint scast_id sint_sbintrunc' wi_hom_pred)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2798
   apply (simp_all add: sint_uint)
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  2799
  done
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2800
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58061
diff changeset
  2801
lemma word_pred_0_n1: "word_pred 0 = word_of_int (- 1)"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  2802
  unfolding word_pred_m1 by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2803
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2804
lemma succ_pred_no [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2805
    "word_succ (numeral w) = numeral w + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2806
    "word_pred (numeral w) = numeral w - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2807
    "word_succ (- numeral w) = - numeral w + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2808
    "word_pred (- numeral w) = - numeral w - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2809
  by (simp_all add: word_succ_p1 word_pred_m1)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2810
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2811
lemma word_sp_01 [simp]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2812
  "word_succ (- 1) = 0 \<and> word_succ 0 = 1 \<and> word_pred 0 = - 1 \<and> word_pred 1 = 0"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2813
  by (simp_all add: word_succ_p1 word_pred_m1)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2814
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2815
\<comment> \<open>alternative approach to lifting arithmetic equalities\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2816
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2817
  by (rule_tac x="uint x" in exI) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2818
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2819
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2820
subsection \<open>Order on fixed-length words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2821
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2822
lift_definition udvd :: \<open>'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool\<close> (infixl \<open>udvd\<close> 50)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2823
  is \<open>\<lambda>k l. take_bit LENGTH('a) k dvd take_bit LENGTH('a) l\<close> by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2824
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2825
lemma udvd_iff_dvd:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2826
  \<open>x udvd y \<longleftrightarrow> unat x dvd unat y\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2827
  by transfer (simp add: nat_dvd_iff)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2828
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2829
lemma udvd_iff_dvd_int:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2830
  \<open>v udvd w \<longleftrightarrow> uint v dvd uint w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2831
  by transfer rule
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2832
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2833
lemma udvdI [intro]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2834
  \<open>v udvd w\<close> if \<open>unat w = unat v * unat u\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2835
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2836
  from that have \<open>unat v dvd unat w\<close> ..
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2837
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2838
    by (simp add: udvd_iff_dvd)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2839
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2840
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2841
lemma udvdE [elim]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2842
  fixes v w :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2843
  assumes \<open>v udvd w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2844
  obtains u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2845
proof (cases \<open>v = 0\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2846
  case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2847
  moreover from True \<open>v udvd w\<close> have \<open>w = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2848
    by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2849
  ultimately show thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2850
    using that by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2851
next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2852
  case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2853
  then have \<open>unat v > 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2854
    by (simp add: unat_gt_0)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2855
  from \<open>v udvd w\<close> have \<open>unat v dvd unat w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2856
    by (simp add: udvd_iff_dvd)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2857
  then obtain n where \<open>unat w = unat v * n\<close> ..
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2858
  moreover have \<open>n < 2 ^ LENGTH('a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2859
  proof (rule ccontr)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2860
    assume \<open>\<not> n < 2 ^ LENGTH('a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2861
    then have \<open>n \<ge> 2 ^ LENGTH('a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2862
      by (simp add: not_le)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2863
    then have \<open>unat v * n \<ge> 2 ^ LENGTH('a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2864
      using \<open>unat v > 0\<close> mult_le_mono [of 1 \<open>unat v\<close> \<open>2 ^ LENGTH('a)\<close> n]
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2865
      by simp
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2866
    with \<open>unat w = unat v * n\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2867
    have \<open>unat w \<ge> 2 ^ LENGTH('a)\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2868
      by simp
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2869
    with unsigned_less [of w, where ?'a = nat] show False
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2870
      by linarith
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2871
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2872
  ultimately have \<open>unat w = unat v * unat (word_of_nat n :: 'a word)\<close>
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  2873
    by (auto simp add: take_bit_nat_eq_self_iff unsigned_of_nat intro: sym)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2874
  with that show thesis .
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2875
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2876
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2877
lemma udvd_imp_mod_eq_0:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2878
  \<open>w mod v = 0\<close> if \<open>v udvd w\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2879
  using that by transfer simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2880
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2881
lemma mod_eq_0_imp_udvd [intro?]:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2882
  \<open>v udvd w\<close> if \<open>w mod v = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2883
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2884
  from that have \<open>unat (w mod v) = unat 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2885
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2886
  then have \<open>unat w mod unat v = 0\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2887
    by (simp add: unat_mod_distrib)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2888
  then have \<open>unat v dvd unat w\<close> ..
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2889
  then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2890
    by (simp add: udvd_iff_dvd)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2891
qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2892
72280
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2893
lemma udvd_imp_dvd:
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2894
  \<open>v dvd w\<close> if \<open>v udvd w\<close> for v w :: \<open>'a::len word\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2895
proof -
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2896
  from that obtain u :: \<open>'a word\<close> where \<open>unat w = unat v * unat u\<close> ..
72280
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2897
  then have \<open>(word_of_nat (unat w) :: 'a word) = word_of_nat (unat v * unat u)\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2898
    by simp
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2899
  then have \<open>w = v * u\<close>
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2900
    by simp
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2901
  then show \<open>v dvd w\<close> ..
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2902
qed
db43ee05066d canonical enum instance for word
haftmann
parents: 72265
diff changeset
  2903
72281
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2904
lemma exp_dvd_iff_exp_udvd:
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2905
  \<open>2 ^ n dvd w \<longleftrightarrow> 2 ^ n udvd w\<close> for v w :: \<open>'a::len word\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2906
proof
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2907
  assume \<open>2 ^ n udvd w\<close> then show \<open>2 ^ n dvd w\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2908
    by (rule udvd_imp_dvd) 
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2909
next
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2910
  assume \<open>2 ^ n dvd w\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2911
  then obtain u :: \<open>'a word\<close> where \<open>w = 2 ^ n * u\<close> ..
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2912
  then have \<open>w = push_bit n u\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2913
    by (simp add: push_bit_eq_mult)
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2914
  then show \<open>2 ^ n udvd w\<close>
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2915
    by transfer (simp add: take_bit_push_bit dvd_eq_mod_eq_0 flip: take_bit_eq_mod)
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2916
qed
beeadb35e357 more thorough treatment of division, particularly signed division on int and word
haftmann
parents: 72280
diff changeset
  2917
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2918
lemma udvd_nat_alt:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2919
  \<open>a udvd b \<longleftrightarrow> (\<exists>n. unat b = n * unat a)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2920
  by (auto simp add: udvd_iff_dvd)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2921
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2922
lemma udvd_unfold_int:
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  2923
  \<open>a udvd b \<longleftrightarrow> (\<exists>n\<ge>0. uint b = n * uint a)\<close>
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
  2924
  unfolding udvd_iff_dvd_int
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
  2925
  by (metis dvd_div_mult_self dvd_triv_right uint_div_distrib uint_ge_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2926
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2927
lemma unat_minus_one:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2928
  \<open>unat (w - 1) = unat w - 1\<close> if \<open>w \<noteq> 0\<close>
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2929
proof -
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2930
  have "0 \<le> uint w" by (fact uint_nonnegative)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2931
  moreover from that have "0 \<noteq> uint w"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2932
    by (simp add: uint_0_iff)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2933
  ultimately have "1 \<le> uint w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2934
    by arith
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2935
  from uint_lt2p [of w] have "uint w - 1 < 2 ^ LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2936
    by arith
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2937
  with \<open>1 \<le> uint w\<close> have "(uint w - 1) mod 2 ^ LENGTH('a) = uint w - 1"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2938
    by (auto intro: mod_pos_pos_trivial)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2939
  with \<open>1 \<le> uint w\<close> have "nat ((uint w - 1) mod 2 ^ LENGTH('a)) = nat (uint w) - 1"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2940
    by (auto simp del: nat_uint_eq)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2941
  then show ?thesis
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2942
    by (simp only: unat_eq_nat_uint word_arith_wis mod_diff_right_eq)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2943
      (metis of_int_1 uint_word_of_int unsigned_1)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2944
qed
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2945
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2946
lemma measure_unat: "p \<noteq> 0 \<Longrightarrow> unat (p - 1) < unat p"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2947
  by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2948
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2949
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2950
lemmas uint_mult_ge0 [simp] = mult_nonneg_nonneg [OF uint_ge_0 uint_ge_0]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2951
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2952
lemma uint_sub_lt2p [simp]: "uint x - uint y < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2953
  for x :: "'a::len word" and y :: "'b::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2954
  using uint_ge_0 [of y] uint_lt2p [of x] by arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2955
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2956
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2957
subsection \<open>Conditions for the addition (etc) of two words to overflow\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2958
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2959
lemma uint_add_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2960
  "(uint x + uint y < 2 ^ LENGTH('a)) =
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2961
    (uint (x + y) = uint x + uint y)"
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
  2962
  for x y :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2963
  by (metis add.right_neutral add_mono_thms_linordered_semiring(1) mod_pos_pos_trivial of_nat_0_le_iff uint_lt2p uint_nat uint_word_ariths(1))
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2964
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2965
lemma uint_mult_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2966
  "(uint x * uint y < 2 ^ LENGTH('a)) =
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2967
    (uint (x * y) = uint x * uint y)"
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
  2968
  for x y :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2969
  by (metis mod_pos_pos_trivial uint_lt2p uint_mult_ge0 uint_word_ariths(3))
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2970
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2971
lemma uint_sub_lem: "uint x \<ge> uint y \<longleftrightarrow> uint (x - y) = uint x - uint y"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  2972
  by (metis diff_ge_0_iff_ge of_nat_0_le_iff uint_nat uint_sub_lt2p uint_word_of_int unique_euclidean_semiring_numeral_class.mod_less word_sub_wi)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2973
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2974
lemma uint_add_le: "uint (x + y) \<le> uint x + uint y"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2975
  unfolding uint_word_ariths by (simp add: zmod_le_nonneg_dividend) 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2976
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2977
lemma uint_sub_ge: "uint (x - y) \<ge> uint x - uint y"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2978
  unfolding uint_word_ariths
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2979
  by (simp flip: take_bit_eq_mod add: take_bit_int_greater_eq_self_iff)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2980
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2981
lemma int_mod_ge: \<open>a \<le> a mod n\<close> if \<open>a < n\<close> \<open>0 < n\<close>
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2982
  for a n :: int
76231
8a48e18f081e reduce prominence of facts
haftmann
parents: 75623
diff changeset
  2983
  using that order.trans [of a 0 \<open>a mod n\<close>] by (cases \<open>a < 0\<close>) auto
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  2984
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2985
lemma mod_add_if_z:
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
  2986
  "\<lbrakk>x < z; y < z; 0 \<le> y; 0 \<le> x; 0 \<le> z\<rbrakk> \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2987
    (x + y) mod z = (if x + y < z then x + y else x + y - z)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2988
  for x y z :: int
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
  2989
  apply (simp add: not_less)
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
  2990
  by (metis (no_types) add_strict_mono diff_ge_0_iff_ge diff_less_eq minus_mod_self2 mod_pos_pos_trivial)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2991
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2992
lemma uint_plus_if':
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2993
  "uint (a + b) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2994
    (if uint a + uint b < 2 ^ LENGTH('a) then uint a + uint b
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2995
     else uint a + uint b - 2 ^ LENGTH('a))"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  2996
  for a b :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2997
  using mod_add_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2998
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2999
lemma mod_sub_if_z:
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
  3000
  "\<lbrakk>x < z; y < z; 0 \<le> y; 0 \<le> x; 0 \<le> z\<rbrakk> \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3001
    (x - y) mod z = (if y \<le> x then x - y else x - y + z)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3002
  for x y z :: int
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
  3003
  using mod_pos_pos_trivial [of "x - y + z" z] by (auto simp add: not_le)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3004
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3005
lemma uint_sub_if':
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3006
  "uint (a - b) =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3007
    (if uint b \<le> uint a then uint a - uint b
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3008
     else uint a - uint b + 2 ^ LENGTH('a))"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3009
  for a b :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3010
  using mod_sub_if_z [of "uint a" _ "uint b"] by (simp add: uint_word_ariths)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3011
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3012
lemma word_of_int_inverse:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3013
  "word_of_int r = a \<Longrightarrow> 0 \<le> r \<Longrightarrow> r < 2 ^ LENGTH('a) \<Longrightarrow> uint a = r"
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
  3014
  for a :: "'a::len word"
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
  3015
  by transfer (simp add: take_bit_int_eq_self)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3016
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3017
lemma unat_split: "P (unat x) \<longleftrightarrow> (\<forall>n. of_nat n = x \<and> n < 2^LENGTH('a) \<longrightarrow> P n)"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3018
  for x :: "'a::len word"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3019
  by (auto simp add: unsigned_of_nat take_bit_nat_eq_self)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3020
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3021
lemma unat_split_asm: "P (unat x) \<longleftrightarrow> (\<nexists>n. of_nat n = x \<and> n < 2^LENGTH('a) \<and> \<not> P n)"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3022
  for x :: "'a::len word"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3023
  by (auto simp add: unsigned_of_nat take_bit_nat_eq_self)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3024
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3025
lemma un_ui_le:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3026
  \<open>unat a \<le> unat b \<longleftrightarrow> uint a \<le> uint b\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3027
  by transfer (simp add: nat_le_iff) 
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3028
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3029
lemma unat_plus_if':
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3030
  \<open>unat (a + b) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3031
    (if unat a + unat b < 2 ^ LENGTH('a)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3032
    then unat a + unat b
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3033
    else unat a + unat b - 2 ^ LENGTH('a))\<close> for a b :: \<open>'a::len word\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3034
  apply (auto simp add: not_less le_iff_add)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3035
   apply (metis (mono_tags, lifting) of_nat_add of_nat_unat take_bit_nat_eq_self_iff unsigned_less unsigned_of_nat unsigned_word_eqI)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3036
  apply (smt (verit, ccfv_SIG) dbl_simps(3) dbl_simps(5) numerals(1) of_nat_0_le_iff of_nat_add of_nat_eq_iff of_nat_numeral of_nat_power of_nat_unat uint_plus_if' unsigned_1)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3037
  done
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3038
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3039
lemma unat_sub_if_size:
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3040
  "unat (x - y) =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3041
    (if unat y \<le> unat x
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3042
     then unat x - unat y
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3043
     else unat x + 2 ^ size x - unat y)"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3044
proof -
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3045
  { assume xy: "\<not> uint y \<le> uint x"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3046
    have "nat (uint x - uint y + 2 ^ LENGTH('a)) = nat (uint x + 2 ^ LENGTH('a) - uint y)"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3047
      by simp
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3048
    also have "... = nat (uint x + 2 ^ LENGTH('a)) - nat (uint y)"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3049
      by (simp add: nat_diff_distrib')
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3050
    also have "... = nat (uint x) + 2 ^ LENGTH('a) - nat (uint y)"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3051
      by (metis nat_add_distrib nat_eq_numeral_power_cancel_iff order_less_imp_le unsigned_0 unsigned_greater_eq unsigned_less)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3052
    finally have "nat (uint x - uint y + 2 ^ LENGTH('a)) = nat (uint x) + 2 ^ LENGTH('a) - nat (uint y)" .
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3053
  }
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3054
  then show ?thesis
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3055
    by (simp add: word_size) (metis nat_diff_distrib' uint_sub_if' un_ui_le unat_eq_nat_uint unsigned_greater_eq)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3056
qed
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3057
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3058
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3059
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3060
lemma uint_split:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3061
  "P (uint x) = (\<forall>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<longrightarrow> P i)"
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
  3062
  for x :: "'a::len word"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3063
  by transfer (auto simp add: take_bit_eq_mod)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3064
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3065
lemma uint_split_asm:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3066
  "P (uint x) = (\<nexists>i. word_of_int i = x \<and> 0 \<le> i \<and> i < 2^LENGTH('a) \<and> \<not> P i)"
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
  3067
  for x :: "'a::len word"
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  3068
  by (auto simp add: unsigned_of_int take_bit_int_eq_self)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3069
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3070
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3071
subsection \<open>Some proof tool support\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3072
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3073
\<comment> \<open>use this to stop, eg. \<open>2 ^ LENGTH(32)\<close> being simplified\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3074
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d"
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3075
  by auto
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3076
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3077
lemmas unat_splits = unat_split unat_split_asm
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3078
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3079
lemmas unat_arith_simps =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3080
  word_le_nat_alt word_less_nat_alt
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3081
  word_unat_eq_iff
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3082
  unat_sub_if' unat_plus_if' unat_div unat_mod
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3083
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3084
lemmas uint_splits = uint_split uint_split_asm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3085
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3086
lemmas uint_arith_simps =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3087
  word_le_def word_less_alt
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3088
  word_uint_eq_iff
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3089
  uint_sub_if' uint_plus_if'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3090
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3091
\<comment> \<open>\<open>unat_arith_tac\<close>: tactic to reduce word arithmetic to \<open>nat\<close>, try to solve via \<open>arith\<close>\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3092
ML \<open>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3093
val unat_arith_simpset =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3094
  @{context} (* TODO: completely explicitly determined simpset *)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3095
  |> fold Simplifier.add_simp @{thms unat_arith_simps}
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3096
  |> fold Splitter.add_split @{thms if_split_asm}
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3097
  |> fold Simplifier.add_cong @{thms power_False_cong}
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3098
  |> simpset_of
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3099
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3100
fun unat_arith_tacs ctxt =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3101
  let
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3102
    fun arith_tac' n t =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3103
      Arith_Data.arith_tac ctxt n t
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3104
        handle Cooper.COOPER _ => Seq.empty;
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3105
  in
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3106
    [ clarify_tac ctxt 1,
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3107
      full_simp_tac (put_simpset unat_arith_simpset ctxt) 1,
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3108
      ALLGOALS (full_simp_tac
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3109
        (put_simpset HOL_ss ctxt
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3110
          |> fold Splitter.add_split @{thms unat_splits}
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3111
          |> fold Simplifier.add_cong @{thms power_False_cong})),
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3112
      rewrite_goals_tac ctxt @{thms word_size},
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3113
      ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3114
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3115
                         REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)),
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3116
      TRYALL arith_tac' ]
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3117
  end
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3118
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3119
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3120
\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3121
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3122
method_setup unat_arith =
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3123
  \<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close>
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3124
  "solving word arithmetic via natural numbers and arith"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3125
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3126
\<comment> \<open>\<open>uint_arith_tac\<close>: reduce to arithmetic on int, try to solve by arith\<close>
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3127
ML \<open>
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3128
val uint_arith_simpset =
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3129
  @{context} (* TODO: completely explicitly determined simpset *)
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3130
  |> fold Simplifier.add_simp @{thms uint_arith_simps}
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3131
  |> fold Splitter.add_split @{thms if_split_asm}
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3132
  |> fold Simplifier.add_cong @{thms power_False_cong}
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3133
  |> simpset_of;
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3134
  
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3135
fun uint_arith_tacs ctxt =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3136
  let
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3137
    fun arith_tac' n t =
59657
2441a80fb6c1 eliminated unused arith "verbose" flag -- tools that need options can use the context;
wenzelm
parents: 59498
diff changeset
  3138
      Arith_Data.arith_tac ctxt n t
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3139
        handle Cooper.COOPER _ => Seq.empty;
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3140
  in
42793
88bee9f6eec7 proper Proof.context for classical tactics;
wenzelm
parents: 41550
diff changeset
  3141
    [ clarify_tac ctxt 1,
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3142
      full_simp_tac (put_simpset uint_arith_simpset ctxt) 1,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  3143
      ALLGOALS (full_simp_tac
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  3144
        (put_simpset HOL_ss ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  3145
          |> fold Splitter.add_split @{thms uint_splits}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  3146
          |> fold Simplifier.add_cong @{thms power_False_cong})),
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3147
      rewrite_goals_tac ctxt @{thms word_size},
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59487
diff changeset
  3148
      ALLGOALS  (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  3149
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3150
                         REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3151
                                 THEN assume_tac ctxt n
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58874
diff changeset
  3152
                                 THEN assume_tac ctxt n)),
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3153
      TRYALL arith_tac' ]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3154
  end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3155
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3156
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3157
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3158
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3159
method_setup uint_arith =
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3160
  \<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3161
  "solving word arithmetic via integers and arith"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3162
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3163
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3164
subsection \<open>More on overflows and monotonicity\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3165
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3166
lemma no_plus_overflow_uint_size: "x \<le> x + y \<longleftrightarrow> uint x + uint y < 2 ^ size x"
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
  3167
  for x y :: "'a::len word"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3168
  by (auto simp add: word_size word_le_def uint_add_lem uint_sub_lem)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3169
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3170
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3171
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3172
lemma no_ulen_sub: "x \<ge> x - y \<longleftrightarrow> uint y \<le> uint x"
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
  3173
  for x y :: "'a::len word"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3174
  by (auto simp add: word_size word_le_def uint_add_lem uint_sub_lem)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3175
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3176
lemma no_olen_add': "x \<le> y + x \<longleftrightarrow> uint y + uint x < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3177
  for x y :: "'a::len word"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  3178
  by (simp add: ac_simps no_olen_add)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3179
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3180
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3181
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3182
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3183
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3184
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3185
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3186
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3187
lemmas word_sub_le = word_sub_le_iff [THEN iffD2]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3188
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3189
lemma word_less_sub1: "x \<noteq> 0 \<Longrightarrow> 1 < x \<longleftrightarrow> 0 < x - 1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3190
  for x :: "'a::len word"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3191
  by transfer (simp add: take_bit_decr_eq) 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3192
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3193
lemma word_le_sub1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> x \<longleftrightarrow> 0 \<le> x - 1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3194
  for x :: "'a::len word"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3195
  by transfer (simp add: int_one_le_iff_zero_less less_le)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3196
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3197
lemma sub_wrap_lt: "x < x - z \<longleftrightarrow> x < z"
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
  3198
  for x z :: "'a::len word"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3199
  by (simp add: word_less_def uint_sub_lem)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3200
   (meson linorder_not_le uint_minus_simple_iff uint_sub_lem word_less_iff_unsigned)
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3201
  
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3202
lemma sub_wrap: "x \<le> x - z \<longleftrightarrow> z = 0 \<or> x < z"
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
  3203
  for x z :: "'a::len word"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3204
  by (simp add: le_less sub_wrap_lt ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3205
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3206
lemma plus_minus_not_NULL_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> c \<noteq> 0 \<Longrightarrow> x + c \<noteq> 0"
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
  3207
  for x ab c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3208
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3209
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3210
lemma plus_minus_no_overflow_ab: "x \<le> ab - c \<Longrightarrow> c \<le> ab \<Longrightarrow> x \<le> x + c"
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
  3211
  for x ab c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3212
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3213
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3214
lemma le_minus': "a + c \<le> b \<Longrightarrow> a \<le> a + c \<Longrightarrow> c \<le> b - a"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3215
  for a b c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3216
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3217
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3218
lemma le_plus': "a \<le> b \<Longrightarrow> c \<le> b - a \<Longrightarrow> a + c \<le> b"
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
  3219
  for a b c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3220
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3221
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3222
lemmas le_plus = le_plus' [rotated]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3223
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  3224
lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3225
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3226
lemma word_plus_mono_right: "y \<le> z \<Longrightarrow> x \<le> x + z \<Longrightarrow> x + y \<le> x + z"
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
  3227
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3228
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3229
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3230
lemma word_less_minus_cancel: "y - x < z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y < z"
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
  3231
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3232
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3233
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3234
lemma word_less_minus_mono_left: "y < z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x < z - x"
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
  3235
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3236
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3237
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3238
lemma word_less_minus_mono: "a < c \<Longrightarrow> d < b \<Longrightarrow> a - b < a \<Longrightarrow> c - d < c \<Longrightarrow> a - b < c - d"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3239
  for a b c d :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3240
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3241
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3242
lemma word_le_minus_cancel: "y - x \<le> z - x \<Longrightarrow> x \<le> z \<Longrightarrow> y \<le> z"
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
  3243
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3244
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3245
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3246
lemma word_le_minus_mono_left: "y \<le> z \<Longrightarrow> x \<le> y \<Longrightarrow> y - x \<le> z - x"
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
  3247
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3248
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3249
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3250
lemma word_le_minus_mono:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3251
  "a \<le> c \<Longrightarrow> d \<le> b \<Longrightarrow> a - b \<le> a \<Longrightarrow> c - d \<le> c \<Longrightarrow> a - b \<le> c - d"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3252
  for a b c d :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3253
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3254
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3255
lemma plus_le_left_cancel_wrap: "x + y' < x \<Longrightarrow> x + y < x \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y"
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
  3256
  for x y y' :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3257
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3258
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3259
lemma plus_le_left_cancel_nowrap: "x \<le> x + y' \<Longrightarrow> x \<le> x + y \<Longrightarrow> x + y' < x + y \<longleftrightarrow> y' < y"
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
  3260
  for x y y' :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3261
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3262
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3263
lemma word_plus_mono_right2: "a \<le> a + b \<Longrightarrow> c \<le> b \<Longrightarrow> a \<le> a + c"
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
  3264
  for a b c :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3265
  by uint_arith
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3266
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3267
lemma word_less_add_right: "x < y - z \<Longrightarrow> z \<le> y \<Longrightarrow> x + z < y"
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
  3268
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3269
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3270
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3271
lemma word_less_sub_right: "x < y + z \<Longrightarrow> y \<le> x \<Longrightarrow> x - y < z"
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
  3272
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3273
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3274
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3275
lemma word_le_plus_either: "x \<le> y \<or> x \<le> z \<Longrightarrow> y \<le> y + z \<Longrightarrow> x \<le> y + z"
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
  3276
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3277
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3278
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3279
lemma word_less_nowrapI: "x < z - k \<Longrightarrow> k \<le> z \<Longrightarrow> 0 < k \<Longrightarrow> x < x + k"
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
  3280
  for x z k :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3281
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3282
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3283
lemma inc_le: "i < m \<Longrightarrow> i + 1 \<le> m"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3284
  for i m :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3285
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3286
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3287
lemma inc_i: "1 \<le> i \<Longrightarrow> i < m \<Longrightarrow> 1 \<le> i + 1 \<and> i + 1 \<le> m"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3288
  for i m :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3289
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3290
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3291
lemma udvd_incr_lem:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3292
  "up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3293
    uq = ua + n' * uint K \<Longrightarrow> up + uint K \<le> uq"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3294
  by auto (metis int_distrib(1) linorder_not_less mult.left_neutral mult_right_mono uint_nonnegative zless_imp_add1_zle)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3295
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3296
lemma udvd_incr':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3297
  "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3298
    uint q = ua + n' * uint K \<Longrightarrow> p + K \<le> q"
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
  3299
  unfolding word_less_alt word_le_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
  3300
  by (metis (full_types) order_trans udvd_incr_lem uint_add_le)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3301
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3302
lemma udvd_decr':
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
  3303
  assumes "p < q" "uint p = ua + n * uint K" "uint q = ua + n' * uint K"
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
  3304
    shows "uint q = ua + n' * uint K \<Longrightarrow> p \<le> q - K"
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
  3305
proof -
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
  3306
  have "\<And>w wa. uint (w::'a word) \<le> uint wa + uint (w - wa)"
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
  3307
    by (metis (no_types) add_diff_cancel_left' diff_add_cancel uint_add_le)
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
  3308
  moreover have "uint K + uint p \<le> uint q"
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
  3309
    using assms by (metis (no_types) add_diff_cancel_left' diff_add_cancel udvd_incr_lem word_less_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
  3310
  ultimately show ?thesis
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
  3311
    by (meson add_le_cancel_left order_trans word_less_eq_iff_unsigned)
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
  3312
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3313
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3314
lemmas udvd_incr_lem0 = udvd_incr_lem [where ua=0, unfolded add_0_left]
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3315
lemmas udvd_incr0 = udvd_incr' [where ua=0, unfolded add_0_left]
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3316
lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3317
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3318
lemma udvd_minus_le': "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy \<le> k - z"
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
  3319
  unfolding udvd_unfold_int
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
  3320
  by (meson udvd_decr0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3321
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3322
lemma udvd_incr2_K:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3323
  "p < a + s \<Longrightarrow> a \<le> a + s \<Longrightarrow> K udvd s \<Longrightarrow> K udvd p - a \<Longrightarrow> a \<le> p \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3324
    0 < K \<Longrightarrow> p \<le> p + K \<and> p + K \<le> a + s"
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
  3325
  unfolding udvd_unfold_int
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  3326
  apply (simp add: uint_arith_simps split: if_split_asm)
73932
fd21b4a93043 added opaque_combs and renamed hide_lams to opaque_lifting
desharna
parents: 73853
diff changeset
  3327
  apply (metis (no_types, opaque_lifting) le_add_diff_inverse le_less_trans udvd_incr_lem)
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
  3328
  using uint_lt2p [of s] by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3329
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3330
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3331
subsection \<open>Arithmetic type class instantiations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3332
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3333
lemmas word_le_0_iff [simp] =
70749
5d06b7bb9d22 More type class generalisations. Note that linorder_antisym_conv1 and linorder_antisym_conv2 no longer exist.
paulson <lp15@cam.ac.uk>
parents: 70342
diff changeset
  3334
  word_zero_le [THEN leD, THEN antisym_conv1]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3335
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3336
lemma word_of_int_nat: "0 \<le> x \<Longrightarrow> word_of_int x = of_nat (nat x)"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3337
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3338
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3339
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3340
  note that \<open>iszero_def\<close> is only for class \<open>comm_semiring_1_cancel\<close>,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3341
  which requires word length \<open>\<ge> 1\<close>, ie \<open>'a::len word\<close>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3342
\<close>
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  3343
lemma iszero_word_no [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3344
  "iszero (numeral bin :: 'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3345
    iszero (take_bit LENGTH('a) (numeral bin :: int))"
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
  3346
  by (metis iszero_def uint_0_iff uint_bintrunc)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3347
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3348
text \<open>Use \<open>iszero\<close> to simplify equalities between word numerals.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3349
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3350
lemmas word_eq_numeral_iff_iszero [simp] =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3351
  eq_numeral_iff_iszero [where 'a="'a::len word"]
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  3352
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3353
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3354
subsection \<open>Word and nat\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3355
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3356
lemma word_nchotomy: "\<forall>w :: 'a::len word. \<exists>n. w = of_nat n \<and> n < 2 ^ LENGTH('a)"
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
  3357
  by (metis of_nat_unat ucast_id unsigned_less)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3358
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3359
lemma of_nat_eq: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ LENGTH('a))"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3360
  for w :: "'a::len word"
68157
057d5b4ce47e removed some non-essential rules
haftmann
parents: 67443
diff changeset
  3361
  using mod_div_mult_eq [of n "2 ^ LENGTH('a)", symmetric]
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  3362
  by (auto simp flip: take_bit_eq_mod simp add: unsigned_of_nat)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3363
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3364
lemma of_nat_eq_size: "of_nat n = w \<longleftrightarrow> (\<exists>q. n = unat w + q * 2 ^ size w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3365
  unfolding word_size by (rule of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3366
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3367
lemma of_nat_0: "of_nat m = (0::'a::len word) \<longleftrightarrow> (\<exists>q. m = q * 2 ^ LENGTH('a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3368
  by (simp add: of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3369
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3370
lemma of_nat_2p [simp]: "of_nat (2 ^ LENGTH('a)) = (0::'a::len word)"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3371
  by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3372
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3373
lemma of_nat_gt_0: "of_nat k \<noteq> 0 \<Longrightarrow> 0 < k"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3374
  by (cases k) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3375
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3376
lemma of_nat_neq_0: "0 < k \<Longrightarrow> k < 2 ^ LENGTH('a::len) \<Longrightarrow> of_nat k \<noteq> (0 :: 'a word)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3377
  by (auto simp add : of_nat_0)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3378
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3379
lemma Abs_fnat_hom_add: "of_nat a + of_nat b = of_nat (a + b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3380
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3381
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3382
lemma Abs_fnat_hom_mult: "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3383
  by (simp add: wi_hom_mult)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3384
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3385
lemma Abs_fnat_hom_Suc: "word_succ (of_nat a) = of_nat (Suc a)"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3386
  by transfer (simp add: ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3387
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3388
lemma Abs_fnat_hom_0: "(0::'a::len word) = of_nat 0"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3389
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3390
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3391
lemma Abs_fnat_hom_1: "(1::'a::len word) = of_nat (Suc 0)"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3392
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3393
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3394
lemmas Abs_fnat_homs =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3395
  Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3396
  Abs_fnat_hom_0 Abs_fnat_hom_1
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3397
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3398
lemma word_arith_nat_add: "a + b = of_nat (unat a + unat b)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3399
  by simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3400
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3401
lemma word_arith_nat_mult: "a * b = of_nat (unat a * unat b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3402
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3403
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3404
lemma word_arith_nat_Suc: "word_succ a = of_nat (Suc (unat a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3405
  by (subst Abs_fnat_hom_Suc [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3406
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3407
lemma word_arith_nat_div: "a div b = of_nat (unat a div unat b)"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3408
  by (metis of_int_of_nat_eq of_nat_unat of_nat_div word_div_def)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3409
  
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3410
lemma word_arith_nat_mod: "a mod b = of_nat (unat a mod unat b)"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3411
  by (metis of_int_of_nat_eq of_nat_mod of_nat_unat word_mod_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3412
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3413
lemmas word_arith_nat_defs =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3414
  word_arith_nat_add word_arith_nat_mult
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3415
  word_arith_nat_Suc Abs_fnat_hom_0
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3416
  Abs_fnat_hom_1 word_arith_nat_div
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3417
  word_arith_nat_mod
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3418
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3419
lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3420
  by (fact arg_cong)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3421
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3422
lemma unat_of_nat:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3423
  \<open>unat (word_of_nat x :: 'a::len word) = x mod 2 ^ LENGTH('a)\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3424
  by transfer (simp flip: take_bit_eq_mod add: nat_take_bit_eq)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3425
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3426
lemmas unat_word_ariths = word_arith_nat_defs
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3427
  [THEN trans [OF unat_cong unat_of_nat]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3428
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3429
lemmas word_sub_less_iff = word_sub_le_iff
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3430
  [unfolded linorder_not_less [symmetric] Not_eq_iff]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3431
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3432
lemma unat_add_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3433
  "unat x + unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x + y) = unat x + unat y"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3434
  for x y :: "'a::len word"
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
  3435
  by (metis mod_less unat_word_ariths(1) unsigned_less)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3436
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3437
lemma unat_mult_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3438
  "unat x * unat y < 2 ^ LENGTH('a) \<longleftrightarrow> unat (x * y) = unat x * unat y"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3439
  for x y :: "'a::len word"
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
  3440
  by (metis mod_less unat_word_ariths(2) unsigned_less)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3441
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3442
lemma le_no_overflow: "x \<le> b \<Longrightarrow> a \<le> a + b \<Longrightarrow> x \<le> a + b"
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
  3443
  for a b x :: "'a::len word"
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
  3444
  using word_le_plus_either by blast
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3445
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3446
lemma uint_div:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3447
  \<open>uint (x div y) = uint x div uint y\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3448
  by (fact uint_div_distrib)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3449
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3450
lemma uint_mod:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3451
  \<open>uint (x mod y) = uint x mod uint y\<close>
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3452
  by (fact uint_mod_distrib)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3453
  
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3454
lemma no_plus_overflow_unat_size: "x \<le> x + y \<longleftrightarrow> unat x + unat y < 2 ^ size x"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3455
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3456
  unfolding word_size by unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3457
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3458
lemmas no_olen_add_nat =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3459
  no_plus_overflow_unat_size [unfolded word_size]
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3460
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3461
lemmas unat_plus_simple =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3462
  trans [OF no_olen_add_nat unat_add_lem]
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3463
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
  3464
lemma word_div_mult: "\<lbrakk>0 < y; unat x * unat y < 2 ^ LENGTH('a)\<rbrakk> \<Longrightarrow> x * y div y = x"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3465
  for x y :: "'a::len word"
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
  3466
  by (simp add: unat_eq_zero unat_mult_lem word_arith_nat_div)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3467
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3468
lemma div_lt': "i \<le> k div x \<Longrightarrow> unat i * unat x < 2 ^ LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3469
  for i k x :: "'a::len word"
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
  3470
  by unat_arith (meson le_less_trans less_mult_imp_div_less not_le unsigned_less)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3471
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3472
lemmas div_lt'' = order_less_imp_le [THEN div_lt']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3473
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
  3474
lemma div_lt_mult: "\<lbrakk>i < k div x; 0 < x\<rbrakk> \<Longrightarrow> i * x < k"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3475
  for i k x :: "'a::len word"
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
  3476
  by (metis div_le_mono div_lt'' not_le unat_div word_div_mult word_less_iff_unsigned)
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
  3477
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
  3478
lemma div_le_mult: "\<lbrakk>i \<le> k div x; 0 < x\<rbrakk> \<Longrightarrow> i * x \<le> k"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3479
  for i k x :: "'a::len word"
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
  3480
  by (metis div_lt' less_mult_imp_div_less not_less unat_arith_simps(2) unat_div unat_mult_lem)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3481
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3482
lemma div_lt_uint': "i \<le> k div x \<Longrightarrow> uint i * uint x < 2 ^ LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3483
  for i k x :: "'a::len word"
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
  3484
  unfolding uint_nat
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
  3485
  by (metis div_lt' int_ops(7) of_nat_unat uint_mult_lem unat_mult_lem)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3486
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3487
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3488
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3489
lemma word_le_exists': "x \<le> y \<Longrightarrow> \<exists>z. y = x + z \<and> uint x + uint z < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3490
  for x y z :: "'a::len word"
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
  3491
  by (metis add.commute diff_add_cancel no_olen_add)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3492
  
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3493
lemmas plus_minus_not_NULL = order_less_imp_le [THEN plus_minus_not_NULL_ab]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3494
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3495
lemmas plus_minus_no_overflow =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3496
  order_less_imp_le [THEN plus_minus_no_overflow_ab]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3497
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3498
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3499
  word_le_minus_cancel word_le_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3500
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3501
lemmas word_l_diffs = mcs [where y = "w + x", unfolded add_diff_cancel] for w x
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3502
lemmas word_diff_ls = mcs [where z = "w + x", unfolded add_diff_cancel] for w x
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3503
lemmas word_plus_mcs = word_diff_ls [where y = "v + x", unfolded add_diff_cancel] for v x
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3504
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3505
lemma le_unat_uoi:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3506
  \<open>y \<le> unat z \<Longrightarrow> unat (word_of_nat y :: 'a word) = y\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3507
  for z :: \<open>'a::len word\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3508
  by transfer (simp add: nat_take_bit_eq take_bit_nat_eq_self_iff le_less_trans)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3509
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66453
diff changeset
  3510
lemmas thd = times_div_less_eq_dividend
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3511
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3512
lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3513
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3514
lemma word_mod_div_equality: "(n div b) * b + (n mod b) = n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3515
  for n b :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3516
  by (fact div_mult_mod_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3517
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3518
lemma word_div_mult_le: "a div b * b \<le> a"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3519
  for a b :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3520
  by (metis div_le_mult mult_not_zero order.not_eq_order_implies_strict order_refl word_zero_le)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3521
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3522
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3523
  for m n :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3524
  by (simp add: unat_arith_simps)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3525
  
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3526
lemma word_of_int_power_hom: "word_of_int a ^ n = (word_of_int (a ^ n) :: 'a::len word)"
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3527
  by (induct n) (simp_all add: wi_hom_mult [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3528
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3529
lemma word_arith_power_alt: "a ^ n = (word_of_int (uint a ^ n) :: 'a::len word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3530
  by (simp add : word_of_int_power_hom [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3531
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3532
lemma unatSuc: "1 + n \<noteq> 0 \<Longrightarrow> unat (1 + n) = Suc (unat n)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3533
  for n :: "'a::len word"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3534
  by unat_arith
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3535
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3536
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3537
subsection \<open>Cardinality, finiteness of set of words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3538
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
  3539
lemma inj_on_word_of_int: \<open>inj_on (word_of_int :: int \<Rightarrow> 'a word) {0..<2 ^ LENGTH('a::len)}\<close>
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
  3540
  unfolding inj_on_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
  3541
  by (metis atLeastLessThan_iff word_of_int_inverse)
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  3542
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
  3543
lemma range_uint: \<open>range (uint :: 'a word \<Rightarrow> int) = {0..<2 ^ LENGTH('a::len)}\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3544
  apply transfer
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3545
  apply (auto simp add: image_iff)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3546
  apply (metis take_bit_int_eq_self_iff)
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3547
  done
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  3548
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
  3549
lemma UNIV_eq: \<open>(UNIV :: 'a word set) = word_of_int ` {0..<2 ^ LENGTH('a::len)}\<close>
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3550
  by (auto simp add: image_iff) (metis atLeastLessThan_iff linorder_not_le uint_split)
45809
2bee94cbae72 finite class instance for word type; remove unused lemmas
huffman
parents: 45808
diff changeset
  3551
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
  3552
lemma card_word: "CARD('a word) = 2 ^ LENGTH('a::len)"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  3553
  by (simp add: UNIV_eq card_image inj_on_word_of_int)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3554
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3555
lemma card_word_size: "CARD('a word) = 2 ^ size x"
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
  3556
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3557
  unfolding word_size by (rule card_word)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3558
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  3559
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  3560
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
  3561
instance word :: (len) finite
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  3562
  by standard (simp add: UNIV_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  3563
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3564
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3565
subsection \<open>Bitwise Operations on Words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3566
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  3567
context
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  3568
  includes bit_operations_syntax
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  3569
begin
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  3570
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  3571
lemma word_wi_log_defs:
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  3572
  "NOT (word_of_int a) = word_of_int (NOT a)"
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  3573
  "word_of_int a AND word_of_int b = word_of_int (a AND b)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  3574
  "word_of_int a OR word_of_int b = word_of_int (a OR b)"
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  3575
  "word_of_int a XOR word_of_int b = word_of_int (a XOR b)"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
  3576
  by (transfer, rule refl)+
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3577
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  3578
lemma word_no_log_defs [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3579
  "NOT (numeral a) = word_of_int (NOT (numeral a))"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3580
  "NOT (- numeral a) = word_of_int (NOT (- numeral a))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3581
  "numeral a AND numeral b = word_of_int (numeral a AND numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3582
  "numeral a AND - numeral b = word_of_int (numeral a AND - numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3583
  "- numeral a AND numeral b = word_of_int (- numeral a AND numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3584
  "- numeral a AND - numeral b = word_of_int (- numeral a AND - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3585
  "numeral a OR numeral b = word_of_int (numeral a OR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3586
  "numeral a OR - numeral b = word_of_int (numeral a OR - numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3587
  "- numeral a OR numeral b = word_of_int (- numeral a OR numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3588
  "- numeral a OR - numeral b = word_of_int (- numeral a OR - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3589
  "numeral a XOR numeral b = word_of_int (numeral a XOR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3590
  "numeral a XOR - numeral b = word_of_int (numeral a XOR - numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3591
  "- numeral a XOR numeral b = word_of_int (- numeral a XOR numeral b)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3592
  "- numeral a XOR - numeral b = word_of_int (- numeral a XOR - numeral b)"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3593
  by (transfer, rule refl)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3594
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3595
text \<open>Special cases for when one of the arguments equals 1.\<close>
46064
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  3596
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  3597
lemma word_bitwise_1_simps [simp]:
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
  3598
  "NOT (1::'a::len word) = -2"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3599
  "1 AND numeral b = word_of_int (1 AND numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3600
  "1 AND - numeral b = word_of_int (1 AND - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3601
  "numeral a AND 1 = word_of_int (numeral a AND 1)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3602
  "- numeral a AND 1 = word_of_int (- numeral a AND 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3603
  "1 OR numeral b = word_of_int (1 OR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3604
  "1 OR - numeral b = word_of_int (1 OR - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3605
  "numeral a OR 1 = word_of_int (numeral a OR 1)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3606
  "- numeral a OR 1 = word_of_int (- numeral a OR 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3607
  "1 XOR numeral b = word_of_int (1 XOR numeral b)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3608
  "1 XOR - numeral b = word_of_int (1 XOR - numeral b)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3609
  "numeral a XOR 1 = word_of_int (numeral a XOR 1)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3610
  "- numeral a XOR 1 = word_of_int (- numeral a XOR 1)"
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  3611
              apply (simp_all add: word_uint_eq_iff unsigned_not_eq unsigned_and_eq unsigned_or_eq
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  3612
         unsigned_xor_eq of_nat_take_bit ac_simps unsigned_of_int)
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3613
       apply (simp_all add: minus_numeral_eq_not_sub_one)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3614
   apply (simp_all only: sub_one_eq_not_neg bit.xor_compl_right take_bit_xor bit.double_compl)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3615
   apply simp_all
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3616
  done
46064
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  3617
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3618
text \<open>Special cases for when one of the arguments equals -1.\<close>
56979
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3619
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3620
lemma word_bitwise_m1_simps [simp]:
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
  3621
  "NOT (-1::'a::len word) = 0"
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
  3622
  "(-1::'a::len word) AND x = x"
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
  3623
  "x AND (-1::'a::len word) = x"
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
  3624
  "(-1::'a::len word) OR x = -1"
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
  3625
  "x OR (-1::'a::len word) = -1"
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
  3626
  " (-1::'a::len word) XOR x = NOT x"
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
  3627
  "x XOR (-1::'a::len word) = NOT x"
56979
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3628
  by (transfer, simp)+
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3629
74163
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3630
lemma word_of_int_not_numeral_eq [simp]:
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3631
  \<open>(word_of_int (NOT (numeral bin)) :: 'a::len word) = - numeral bin - 1\<close>
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3632
  by transfer (simp add: not_eq_complement)
afe3c8ae1624 consolidation of rules for bit operations
haftmann
parents: 74108
diff changeset
  3633
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3634
lemma uint_and:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3635
  \<open>uint (x AND y) = uint x AND uint y\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3636
  by transfer simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3637
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3638
lemma uint_or:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3639
  \<open>uint (x OR y) = uint x OR uint y\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3640
  by transfer simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3641
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3642
lemma uint_xor:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3643
  \<open>uint (x XOR y) = uint x XOR uint y\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3644
  by transfer simp
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3645
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3646
\<comment> \<open>get from commutativity, associativity etc of \<open>int_and\<close> etc to same for \<open>word_and etc\<close>\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3647
lemmas bwsimps =
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
  3648
  wi_hom_add
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3649
  word_wi_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3650
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3651
lemma word_bw_assocs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3652
  "(x AND y) AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3653
  "(x OR y) OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3654
  "(x XOR y) XOR z = x XOR y XOR z"
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
  3655
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3656
  by (fact ac_simps)+
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3657
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3658
lemma word_bw_comms:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3659
  "x AND y = y AND x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3660
  "x OR y = y OR x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3661
  "x XOR y = y XOR x"
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
  3662
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3663
  by (fact ac_simps)+
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3664
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3665
lemma word_bw_lcs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3666
  "y AND x AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3667
  "y OR x OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3668
  "y XOR x XOR z = x XOR y XOR z"
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
  3669
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3670
  by (fact ac_simps)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3671
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3672
lemma word_log_esimps:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3673
  "x AND 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3674
  "x AND -1 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3675
  "x OR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3676
  "x OR -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3677
  "x XOR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3678
  "x XOR -1 = NOT x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3679
  "0 AND x = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3680
  "-1 AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3681
  "0 OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3682
  "-1 OR x = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3683
  "0 XOR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3684
  "-1 XOR x = NOT x"
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
  3685
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3686
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3687
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3688
lemma word_not_dist:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3689
  "NOT (x OR y) = NOT x AND NOT y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3690
  "NOT (x AND y) = NOT x OR NOT y"
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
  3691
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3692
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3693
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3694
lemma word_bw_same:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3695
  "x AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3696
  "x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3697
  "x XOR x = 0"
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
  3698
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3699
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3700
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3701
lemma word_ao_absorbs [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3702
  "x AND (y OR x) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3703
  "x OR y AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3704
  "x AND (x OR y) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3705
  "y AND x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3706
  "(y OR x) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3707
  "x OR x AND y = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3708
  "(x OR y) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3709
  "x AND y OR x = x"
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
  3710
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3711
  by (auto intro: bit_eqI simp add: bit_and_iff bit_or_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3712
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  3713
lemma word_not_not [simp]: "NOT (NOT x) = x"
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
  3714
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3715
  by (fact bit.double_compl)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3716
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3717
lemma word_ao_dist: "(x OR y) AND z = x AND z OR y AND z"
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
  3718
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3719
  by (fact bit.conj_disj_distrib2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3720
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3721
lemma word_oa_dist: "x AND y OR z = (x OR z) AND (y OR z)"
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
  3722
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3723
  by (fact bit.disj_conj_distrib2)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3724
  
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3725
lemma word_add_not [simp]: "x + NOT x = -1"
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
  3726
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3727
  by (simp add: not_eq_complement)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  3728
  
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3729
lemma word_plus_and_or [simp]: "(x AND y) + (x OR y) = x + y"
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
  3730
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3731
  by transfer (simp add: plus_and_or)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3732
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3733
lemma leoa: "w = x OR y \<Longrightarrow> y = w AND y"
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
  3734
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3735
  by auto
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3736
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3737
lemma leao: "w' = x' AND y' \<Longrightarrow> x' = x' OR w'"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3738
  for x' :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3739
  by auto
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3740
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3741
lemma word_ao_equiv: "w = w OR w' \<longleftrightarrow> w' = w AND w'"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3742
  for w w' :: "'a::len word"
48196
b7313810b6e6 explicit is better than implicit;
wenzelm
parents: 47941
diff changeset
  3743
  by (auto intro: leoa leao)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3744
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3745
lemma le_word_or2: "x \<le> x OR y"
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
  3746
  for x y :: "'a::len word"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3747
  by (simp add: or_greater_eq uint_or word_le_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3748
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3749
lemmas le_word_or1 = xtrans(3) [OF word_bw_comms (2) le_word_or2]
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3750
lemmas word_and_le1 = xtrans(3) [OF word_ao_absorbs (4) [symmetric] le_word_or2]
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3751
lemmas word_and_le2 = xtrans(3) [OF word_ao_absorbs (8) [symmetric] le_word_or2]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3752
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  3753
lemma bit_horner_sum_bit_word_iff [bit_simps]:
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3754
  \<open>bit (horner_sum of_bool (2 :: 'a::len word) bs) n
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3755
    \<longleftrightarrow> n < min LENGTH('a) (length bs) \<and> bs ! n\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3756
  by transfer (simp add: bit_horner_sum_bit_iff)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3757
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3758
definition word_reverse :: \<open>'a::len word \<Rightarrow> 'a word\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3759
  where \<open>word_reverse w = horner_sum of_bool 2 (rev (map (bit w) [0..<LENGTH('a)]))\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3760
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  3761
lemma bit_word_reverse_iff [bit_simps]:
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3762
  \<open>bit (word_reverse w) n \<longleftrightarrow> n < LENGTH('a) \<and> bit w (LENGTH('a) - Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3763
  for w :: \<open>'a::len word\<close>
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3764
  by (cases \<open>n < LENGTH('a)\<close>)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3765
    (simp_all add: word_reverse_def bit_horner_sum_bit_word_iff rev_nth)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3766
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3767
lemma word_rev_rev [simp] : "word_reverse (word_reverse w) = w"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3768
  by (rule bit_word_eqI)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3769
    (auto simp add: bit_word_reverse_iff bit_imp_le_length Suc_diff_Suc)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3770
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3771
lemma word_rev_gal: "word_reverse w = u \<Longrightarrow> word_reverse u = w"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3772
  by (metis word_rev_rev)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3773
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3774
lemma word_rev_gal': "u = word_reverse w \<Longrightarrow> w = word_reverse u"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3775
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3776
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3777
lemma uint_2p: "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n"
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
  3778
  by (cases \<open>n < LENGTH('a)\<close>; transfer; force)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3779
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3780
lemma word_of_int_2p: "(word_of_int (2 ^ n) :: 'a::len word) = 2 ^ n"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  3781
  by (induct n) (simp_all add: wi_hom_syms)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3782
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3783
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3784
subsubsection \<open>shift functions in terms of lists of bools\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3785
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3786
lemma drop_bit_word_numeral [simp]:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3787
  \<open>drop_bit (numeral n) (numeral k) =
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3788
    (word_of_int (drop_bit (numeral n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3789
  by transfer simp
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3790
74498
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3791
lemma drop_bit_word_Suc_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3792
  \<open>drop_bit (Suc n) (numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3793
    (word_of_int (drop_bit (Suc n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3794
  by transfer simp
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3795
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3796
lemma drop_bit_word_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3797
  \<open>drop_bit (numeral n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3798
    (word_of_int (drop_bit (numeral n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3799
  by transfer simp
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3800
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3801
lemma drop_bit_word_Suc_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3802
  \<open>drop_bit (Suc n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3803
    (word_of_int (drop_bit (Suc n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3804
  by transfer simp
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3805
73853
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  3806
lemma signed_drop_bit_word_numeral [simp]:
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  3807
  \<open>signed_drop_bit (numeral n) (numeral k) =
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  3808
    (word_of_int (drop_bit (numeral n) (signed_take_bit (LENGTH('a) - 1) (numeral k))) :: 'a::len word)\<close>
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  3809
  by transfer simp
52b829b18066 more lemmas
haftmann
parents: 73816
diff changeset
  3810
74498
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3811
lemma signed_drop_bit_word_Suc_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3812
  \<open>signed_drop_bit (Suc n) (numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3813
    (word_of_int (drop_bit (Suc n) (signed_take_bit (LENGTH('a) - 1) (numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3814
  by transfer simp
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3815
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3816
lemma signed_drop_bit_word_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3817
  \<open>signed_drop_bit (numeral n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3818
    (word_of_int (drop_bit (numeral n) (signed_take_bit (LENGTH('a) - 1) (- numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3819
  by transfer simp
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3820
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3821
lemma signed_drop_bit_word_Suc_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3822
  \<open>signed_drop_bit (Suc n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3823
    (word_of_int (drop_bit (Suc n) (signed_take_bit (LENGTH('a) - 1) (- numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3824
  by transfer simp
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3825
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3826
lemma take_bit_word_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3827
  \<open>take_bit (numeral n) (numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3828
    (word_of_int (take_bit (min LENGTH('a) (numeral n)) (numeral k)) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3829
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3830
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3831
lemma take_bit_word_Suc_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3832
  \<open>take_bit (Suc n) (numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3833
    (word_of_int (take_bit (min LENGTH('a) (Suc n)) (numeral k)) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3834
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3835
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3836
lemma take_bit_word_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3837
  \<open>take_bit (numeral n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3838
    (word_of_int (take_bit (min LENGTH('a) (numeral n)) (- numeral k)) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3839
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3840
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3841
lemma take_bit_word_Suc_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3842
  \<open>take_bit (Suc n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3843
    (word_of_int (take_bit (min LENGTH('a) (Suc n)) (- numeral k)) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3844
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3845
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3846
lemma signed_take_bit_word_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3847
  \<open>signed_take_bit (numeral n) (numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3848
    (word_of_int (signed_take_bit (numeral n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3849
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3850
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3851
lemma signed_take_bit_word_Suc_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3852
  \<open>signed_take_bit (Suc n) (numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3853
    (word_of_int (signed_take_bit (Suc n) (take_bit LENGTH('a) (numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3854
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3855
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3856
lemma signed_take_bit_word_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3857
  \<open>signed_take_bit (numeral n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3858
    (word_of_int (signed_take_bit (numeral n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3859
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3860
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3861
lemma signed_take_bit_word_Suc_minus_numeral [simp]:
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3862
  \<open>signed_take_bit (Suc n) (- numeral k) =
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3863
    (word_of_int (signed_take_bit (Suc n) (take_bit LENGTH('a) (- numeral k))) :: 'a::len word)\<close>
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3864
  by transfer rule
27475e64a887 more complete simp rules
haftmann
parents: 74496
diff changeset
  3865
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
  3866
lemma False_map2_or: "\<lbrakk>set xs \<subseteq> {False}; length ys = length xs\<rbrakk> \<Longrightarrow> map2 (\<or>) xs ys = ys"
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
  3867
  by (induction xs arbitrary: ys) (auto simp: length_Suc_conv)
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
  3868
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
  3869
lemma align_lem_or:
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
  3870
  assumes "length xs = n + m" "length ys = n + m" 
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
  3871
    and "drop m xs = replicate n False" "take m ys = replicate m False"
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
  3872
  shows "map2 (\<or>) xs ys = take m xs @ drop m ys"
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
  3873
  using assms
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
  3874
proof (induction xs arbitrary: ys m)
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
  3875
  case (Cons a xs)
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
  3876
  then show ?case
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
  3877
    by (cases m) (auto simp: length_Suc_conv False_map2_or)
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
  3878
qed auto
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
  3879
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
  3880
lemma False_map2_and: "\<lbrakk>set xs \<subseteq> {False}; length ys = length xs\<rbrakk> \<Longrightarrow> map2 (\<and>) xs ys = xs"
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
  3881
  by (induction xs arbitrary: ys) (auto simp: length_Suc_conv)
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
  3882
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
  3883
lemma align_lem_and:
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
  3884
  assumes "length xs = n + m" "length ys = n + m" 
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
  3885
    and "drop m xs = replicate n False" "take m ys = replicate m False"
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
  3886
  shows "map2 (\<and>) xs ys = replicate (n + m) False"
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
  3887
  using assms
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
  3888
proof (induction xs arbitrary: ys m)
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
  3889
  case (Cons a xs)
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
  3890
  then show ?case
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
  3891
    by (cases m) (auto simp: length_Suc_conv set_replicate_conv_if False_map2_and)
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
  3892
qed auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3893
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3894
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3895
subsubsection \<open>Mask\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3896
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3897
lemma minus_1_eq_mask:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3898
  \<open>- 1 = (mask LENGTH('a) :: 'a::len word)\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  3899
  by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3900
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3901
lemma mask_eq_decr_exp:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3902
  \<open>mask n = 2 ^ n - (1 :: 'a::len word)\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3903
  by (fact mask_eq_exp_minus_1)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3904
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3905
lemma mask_Suc_rec:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3906
  \<open>mask (Suc n) = 2 * mask n + (1 :: 'a::len word)\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3907
  by (simp add: mask_eq_exp_minus_1)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3908
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3909
context
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3910
begin
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3911
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  3912
qualified lemma bit_mask_iff [bit_simps]:
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3913
  \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < min LENGTH('a) m\<close>
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  3914
  by (simp add: bit_mask_iff not_le)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3915
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3916
end
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3917
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3918
lemma mask_bin: "mask n = word_of_int (take_bit n (- 1))"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  3919
  by transfer simp 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3920
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3921
lemma and_mask_bintr: "w AND mask n = word_of_int (take_bit n (uint w))"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3922
  by transfer (simp add: ac_simps take_bit_eq_mask)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3923
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3924
lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (take_bit n i)"
74496
807b094a9b78 avoid overaggressive contraction of conversions
haftmann
parents: 74391
diff changeset
  3925
  by (simp add: take_bit_eq_mask of_int_and_eq of_int_mask_eq)
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3926
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3927
lemma and_mask_wi':
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3928
  "word_of_int i AND mask n = (word_of_int (take_bit (min LENGTH('a) n) i) :: 'a::len word)"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  3929
  by (auto simp add: and_mask_wi min_def wi_bintr)
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  3930
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3931
lemma and_mask_no: "numeral i AND mask n = word_of_int (take_bit n (numeral i))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3932
  unfolding word_numeral_alt by (rule and_mask_wi)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3933
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3934
lemma and_mask_mod_2p: "w AND mask n = word_of_int (uint w mod 2 ^ n)"
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3935
  by (simp only: and_mask_bintr take_bit_eq_mod)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3936
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3937
lemma uint_mask_eq:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3938
  \<open>uint (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3939
  by transfer simp
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3940
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3941
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n"
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
  3942
  by (metis take_bit_eq_mask take_bit_int_less_exp unsigned_take_bit_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3943
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3944
lemma mask_eq_iff: "w AND mask n = w \<longleftrightarrow> uint w < 2 ^ n"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3945
  apply (auto simp flip: take_bit_eq_mask)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3946
   apply (metis take_bit_int_eq_self_iff uint_take_bit_eq)
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  3947
  apply (simp add: take_bit_int_eq_self unsigned_take_bit_eq word_uint_eqI)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3948
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3949
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3950
lemma and_mask_dvd: "2 ^ n dvd uint w \<longleftrightarrow> w AND mask n = 0"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3951
  by (simp flip: take_bit_eq_mask take_bit_eq_mod unsigned_take_bit_eq add: dvd_eq_mod_eq_0 uint_0_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3952
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3953
lemma and_mask_dvd_nat: "2 ^ n dvd unat w \<longleftrightarrow> w AND mask n = 0"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3954
  by (simp flip: take_bit_eq_mask take_bit_eq_mod unsigned_take_bit_eq add: dvd_eq_mod_eq_0 unat_0_iff uint_0_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3955
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3956
lemma word_2p_lem: "n < size w \<Longrightarrow> w < 2 ^ n = (uint w < 2 ^ n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3957
  for w :: "'a::len word"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  3958
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3959
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
  3960
lemma less_mask_eq:
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
  3961
  fixes x :: "'a::len word"
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
  3962
  assumes "x < 2 ^ n" shows "x AND mask n = x"
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
  3963
  by (metis (no_types) assms lt2p_lem mask_eq_iff not_less word_2p_lem word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3964
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3965
lemmas mask_eq_iff_w2p = trans [OF mask_eq_iff word_2p_lem [symmetric]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3966
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3967
lemmas and_mask_less' = iffD2 [OF word_2p_lem and_mask_lt_2p, simplified word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3968
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3969
lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2 ^ n"
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3970
  for x :: \<open>'a::len word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3971
  unfolding word_size by (erule and_mask_less')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3972
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3973
lemma word_mod_2p_is_mask [OF refl]: "c = 2 ^ n \<Longrightarrow> c > 0 \<Longrightarrow> x mod c = x AND mask n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3974
  for c x :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3975
  by (auto simp: word_mod_def uint_2p and_mask_mod_2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3976
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3977
lemma mask_eqs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3978
  "(a AND mask n) + b AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3979
  "a + (b AND mask n) AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3980
  "(a AND mask n) - b AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3981
  "a - (b AND mask n) AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3982
  "a * (b AND mask n) AND mask n = a * b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3983
  "(b AND mask n) * a AND mask n = b * a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3984
  "(a AND mask n) + (b AND mask n) AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3985
  "(a AND mask n) - (b AND mask n) AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3986
  "(a AND mask n) * (b AND mask n) AND mask n = a * b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3987
  "- (a AND mask n) AND mask n = - a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3988
  "word_succ (a AND mask n) AND mask n = word_succ a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3989
  "word_pred (a AND mask n) AND mask n = word_pred a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3990
  using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b]
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
  3991
  unfolding take_bit_eq_mask [symmetric]
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
  3992
  by (transfer; simp add: take_bit_eq_mod mod_simps)+
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3993
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3994
lemma mask_power_eq: "(x AND mask n) ^ k AND mask n = x ^ k AND mask n"
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3995
  for x :: \<open>'a::len word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3996
  using word_of_int_Ex [where x=x]
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
  3997
  unfolding take_bit_eq_mask [symmetric]
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
  3998
  by (transfer; simp add: take_bit_eq_mod mod_simps)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3999
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4000
lemma mask_full [simp]: "mask LENGTH('a) = (- 1 :: 'a::len word)"
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  4001
  by transfer simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4002
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4003
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4004
subsubsection \<open>Slices\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4005
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4006
definition slice1 :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4007
  where \<open>slice1 n w = (if n < LENGTH('a)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4008
    then ucast (drop_bit (LENGTH('a) - n) w)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4009
    else push_bit (n - LENGTH('a)) (ucast w))\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4010
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  4011
lemma bit_slice1_iff [bit_simps]:
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4012
  \<open>bit (slice1 m w :: 'b::len word) n \<longleftrightarrow> m - LENGTH('a) \<le> n \<and> n < min LENGTH('b) m
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4013
    \<and> bit w (n + (LENGTH('a) - m) - (m - LENGTH('a)))\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4014
  for w :: \<open>'a::len word\<close>
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
  4015
  by (auto simp add: slice1_def bit_ucast_iff bit_drop_bit_eq bit_push_bit_iff not_less not_le ac_simps
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4016
    dest: bit_imp_le_length)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4017
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4018
definition slice :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4019
  where \<open>slice n = slice1 (LENGTH('a) - n)\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4020
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  4021
lemma bit_slice_iff [bit_simps]:
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4022
  \<open>bit (slice m w :: 'b::len word) n \<longleftrightarrow> n < min LENGTH('b) (LENGTH('a) - m) \<and> bit w (n + LENGTH('a) - (LENGTH('a) - m))\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4023
  for w :: \<open>'a::len word\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4024
  by (simp add: slice_def word_size bit_slice1_iff)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4025
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4026
lemma slice1_0 [simp] : "slice1 n 0 = 0"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4027
  unfolding slice1_def by simp
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4028
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4029
lemma slice_0 [simp] : "slice n 0 = 0"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4030
  unfolding slice_def by auto
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4031
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4032
lemma ucast_slice1: "ucast w = slice1 (size w) w"
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
  4033
  unfolding slice1_def by (simp add: size_word.rep_eq)
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4034
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4035
lemma ucast_slice: "ucast w = slice 0 w"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4036
  by (simp add: slice_def slice1_def)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4037
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4038
lemma slice_id: "slice 0 t = t"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4039
  by (simp only: ucast_slice [symmetric] ucast_id)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4040
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4041
lemma rev_slice1:
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4042
  \<open>slice1 n (word_reverse w :: 'b::len word) = word_reverse (slice1 k w :: 'a::len word)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4043
  if \<open>n + k = LENGTH('a) + LENGTH('b)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4044
proof (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4045
  fix m
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4046
  assume *: \<open>m < LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4047
  from that have **: \<open>LENGTH('b) = n + k - LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4048
    by simp
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4049
  show \<open>bit (slice1 n (word_reverse w :: 'b word) :: 'a word) m \<longleftrightarrow> bit (word_reverse (slice1 k w :: 'a word)) m\<close>
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
  4050
    unfolding bit_slice1_iff bit_word_reverse_iff
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4051
    using * **
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
  4052
    by (cases \<open>n \<le> LENGTH('a)\<close>; cases \<open>k \<le> LENGTH('a)\<close>) auto
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4053
qed
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4054
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4055
lemma rev_slice:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4056
  "n + k + LENGTH('a::len) = LENGTH('b::len) \<Longrightarrow>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4057
    slice n (word_reverse (w::'b word)) = word_reverse (slice k w :: 'a word)"
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
  4058
  unfolding slice_def word_size
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
  4059
  by (simp add: rev_slice1)
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4060
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4061
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4062
subsubsection \<open>Revcast\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4063
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4064
definition revcast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4065
  where \<open>revcast = slice1 LENGTH('b)\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4066
72611
c7bc3e70a8c7 official collection for bit projection simplifications
haftmann
parents: 72515
diff changeset
  4067
lemma bit_revcast_iff [bit_simps]:
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4068
  \<open>bit (revcast w :: 'b::len word) n \<longleftrightarrow> LENGTH('b) - LENGTH('a) \<le> n \<and> n < LENGTH('b)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4069
    \<and> bit w (n + (LENGTH('a) - LENGTH('b)) - (LENGTH('b) - LENGTH('a)))\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4070
  for w :: \<open>'a::len word\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4071
  by (simp add: revcast_def bit_slice1_iff)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4072
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4073
lemma revcast_slice1 [OF refl]: "rc = revcast w \<Longrightarrow> slice1 (size rc) w = rc"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4074
  by (simp add: revcast_def word_size)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  4075
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4076
lemma revcast_rev_ucast [OF refl refl refl]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4077
  "cs = [rc, uc] \<Longrightarrow> rc = revcast (word_reverse w) \<Longrightarrow> uc = ucast w \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4078
    rc = word_reverse uc"
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
  4079
  by (metis rev_slice1 revcast_slice1 ucast_slice1 word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4080
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4081
lemma revcast_ucast: "revcast w = word_reverse (ucast (word_reverse w))"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4082
  using revcast_rev_ucast [of "word_reverse w"] by simp
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4083
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4084
lemma ucast_revcast: "ucast w = word_reverse (revcast (word_reverse w))"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4085
  by (fact revcast_rev_ucast [THEN word_rev_gal'])
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4086
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4087
lemma ucast_rev_revcast: "ucast (word_reverse w) = word_reverse (revcast w)"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  4088
  by (fact revcast_ucast [THEN word_rev_gal'])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4089
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4090
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  4091
text "linking revcast and cast via shift"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4092
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4093
lemmas wsst_TYs = source_size target_size word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4094
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4095
lemmas sym_notr =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4096
  not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4097
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4098
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4099
subsection \<open>Split and cat\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4100
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4101
lemmas word_split_bin' = word_split_def
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4102
lemmas word_cat_bin' = word_cat_eq
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4103
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4104
\<comment> \<open>this odd result is analogous to \<open>ucast_id\<close>,
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4105
      result to the length given by the result type\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4106
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4107
lemma word_cat_id: "word_cat a b = b"
72488
ee659bca8955 factored out theory Bits_Int
haftmann
parents: 72487
diff changeset
  4108
  by transfer (simp add: take_bit_concat_bit_eq)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4109
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
  4110
lemma word_cat_split_alt: "\<lbrakk>size w \<le> size u + size v; word_split w = (u,v)\<rbrakk> \<Longrightarrow> word_cat u v = w"
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
  4111
  unfolding word_split_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
  4112
  by (rule bit_word_eqI) (auto simp add: bit_word_cat_iff not_less word_size bit_ucast_iff bit_drop_bit_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4113
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  4114
lemmas word_cat_split_size = sym [THEN [2] word_cat_split_alt [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4115
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4116
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4117
subsubsection \<open>Split and slice\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4118
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
  4119
lemma split_slices:
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
  4120
  assumes "word_split w = (u, v)"
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
  4121
  shows "u = slice (size v) w \<and> v = slice 0 w"
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
  4122
  unfolding word_size
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
  4123
proof (intro conjI)
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
  4124
  have \<section>: "\<And>n. \<lbrakk>ucast (drop_bit LENGTH('b) w) = u; LENGTH('c) < LENGTH('b)\<rbrakk> \<Longrightarrow> \<not> bit u 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
  4125
    by (metis bit_take_bit_iff bit_word_of_int_iff diff_is_0_eq' drop_bit_take_bit less_imp_le less_nat_zero_code of_int_uint unsigned_drop_bit_eq)
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
  4126
  show "u = slice LENGTH('b) w"
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
  4127
  proof (rule bit_word_eqI)
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
  4128
    show "bit u n = bit ((slice LENGTH('b) w)::'a word) n" if "n < LENGTH('a)" for 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
  4129
      using assms bit_imp_le_length
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
  4130
      unfolding word_split_def bit_slice_iff
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
  4131
      by (fastforce simp add: \<section> ac_simps word_size bit_ucast_iff bit_drop_bit_eq)
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
  4132
  qed
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
  4133
  show "v = slice 0 w"
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
  4134
    by (metis Pair_inject assms ucast_slice word_split_bin')
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
  4135
qed
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
  4136
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4137
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4138
lemma slice_cat1 [OF refl]:
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
  4139
  "\<lbrakk>wc = word_cat a b; size a + size b \<le> size wc\<rbrakk> \<Longrightarrow> slice (size b) wc = a"
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
  4140
  by (rule bit_word_eqI) (auto simp add: bit_slice_iff bit_word_cat_iff word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4141
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4142
lemmas slice_cat2 = trans [OF slice_id word_cat_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4143
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4144
lemma cat_slices:
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
  4145
  "\<lbrakk>a = slice n c; b = slice 0 c; n = size b; size c \<le> size a + size b\<rbrakk> \<Longrightarrow> word_cat a b = c"
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
  4146
  by (rule bit_word_eqI) (auto simp add: bit_slice_iff bit_word_cat_iff word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4147
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4148
lemma word_split_cat_alt:
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
  4149
  assumes "w = word_cat u v" and size: "size u + size v \<le> size w"
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
  4150
  shows "word_split w = (u,v)"
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
  4151
proof -
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
  4152
  have "ucast ((drop_bit LENGTH('c) (word_cat u v))::'a word) = u" "ucast ((word_cat u v)::'a word) = v"
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
  4153
    using assms
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
  4154
    by (auto simp add: word_size bit_ucast_iff bit_drop_bit_eq bit_word_cat_iff intro: bit_eqI)
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
  4155
  then show ?thesis
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
  4156
    by (simp add: assms(1) word_split_bin')
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
  4157
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4158
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4159
lemma horner_sum_uint_exp_Cons_eq:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4160
  \<open>horner_sum uint (2 ^ LENGTH('a)) (w # ws) =
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4161
    concat_bit LENGTH('a) (uint w) (horner_sum uint (2 ^ LENGTH('a)) ws)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4162
  for ws :: \<open>'a::len word list\<close>
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
  4163
  by (simp add: bintr_uint concat_bit_eq push_bit_eq_mult)
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4164
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4165
lemma bit_horner_sum_uint_exp_iff:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4166
  \<open>bit (horner_sum uint (2 ^ LENGTH('a)) ws) n \<longleftrightarrow>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4167
    n div LENGTH('a) < length ws \<and> bit (ws ! (n div LENGTH('a))) (n mod LENGTH('a))\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4168
  for ws :: \<open>'a::len word list\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4169
proof (induction ws arbitrary: n)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4170
  case Nil
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4171
  then show ?case
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4172
    by simp
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4173
next
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4174
  case (Cons w ws)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4175
  then show ?case
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4176
    by (cases \<open>n \<ge> LENGTH('a)\<close>)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4177
      (simp_all only: horner_sum_uint_exp_Cons_eq, simp_all add: bit_concat_bit_iff le_div_geq le_mod_geq bit_uint_iff Cons)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4178
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4179
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4180
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4181
subsection \<open>Rotation\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4182
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
  4183
lemma word_rotr_word_rotr_eq: \<open>word_rotr m (word_rotr n w) = word_rotr (m + n) w\<close>
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4184
  by (rule bit_word_eqI) (simp add: bit_word_rotr_iff ac_simps mod_add_right_eq)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4185
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
  4186
lemma word_rot_lem: "\<lbrakk>l + k = d + k mod l; n < l\<rbrakk> \<Longrightarrow> ((d + n) mod l) = n" for l::nat
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
  4187
  by (metis (no_types, lifting) add.commute add.right_neutral add_diff_cancel_left' mod_if mod_mult_div_eq mod_mult_self2 mod_self)
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
  4188
 
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
  4189
lemma word_rot_rl [simp]: \<open>word_rotl k (word_rotr k v) = v\<close>
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
  4190
proof (rule bit_word_eqI)
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
  4191
  show "bit (word_rotl k (word_rotr k v)) n = bit v n" if "n < LENGTH('a)" for 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
  4192
    using that
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
  4193
    by (auto simp: word_rot_lem word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps split: nat_diff_split)
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
  4194
qed
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
  4195
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
  4196
lemma word_rot_lr [simp]: \<open>word_rotr k (word_rotl k v) = v\<close>
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
  4197
proof (rule bit_word_eqI)
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
  4198
  show "bit (word_rotr k (word_rotl k v)) n = bit v n" if "n < LENGTH('a)" for 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
  4199
    using that
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
  4200
    by (auto simp add: word_rot_lem word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps split: nat_diff_split)
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
  4201
qed
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4202
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4203
lemma word_rot_gal:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4204
  \<open>word_rotr n v = w \<longleftrightarrow> word_rotl n w = v\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4205
  by auto
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4206
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4207
lemma word_rot_gal':
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4208
  \<open>w = word_rotr n v \<longleftrightarrow> v = word_rotl n w\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4209
  by auto
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4210
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4211
lemma word_rotr_rev:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4212
  \<open>word_rotr n w = word_reverse (word_rotl n (word_reverse w))\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4213
proof (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4214
  fix m
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4215
  assume \<open>m < LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4216
  moreover have \<open>1 +
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4217
    ((int m + int n mod int LENGTH('a)) mod int LENGTH('a) +
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4218
     ((int LENGTH('a) * 2) mod int LENGTH('a) - (1 + (int m + int n mod int LENGTH('a)))) mod int LENGTH('a)) =
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4219
    int LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4220
    apply (cases \<open>(1 + (int m + int n mod int LENGTH('a))) mod
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4221
         int LENGTH('a) = 0\<close>)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4222
    using zmod_zminus1_eq_if [of \<open>1 + (int m + int n mod int LENGTH('a))\<close> \<open>int LENGTH('a)\<close>]
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4223
    apply simp_all
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4224
     apply (auto simp add: algebra_simps)
73932
fd21b4a93043 added opaque_combs and renamed hide_lams to opaque_lifting
desharna
parents: 73853
diff changeset
  4225
    apply (metis (mono_tags, opaque_lifting) Abs_fnat_hom_add mod_Suc mod_mult_self2_is_0 of_nat_Suc of_nat_mod semiring_char_0_class.of_nat_neq_0)
fd21b4a93043 added opaque_combs and renamed hide_lams to opaque_lifting
desharna
parents: 73853
diff changeset
  4226
    apply (metis (no_types, opaque_lifting) Abs_fnat_hom_add less_not_refl mod_Suc of_nat_Suc of_nat_gt_0 of_nat_mod)
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4227
    done
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4228
  then have \<open>int ((m + n) mod LENGTH('a)) =
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4229
    int (LENGTH('a) - Suc ((LENGTH('a) - Suc m + LENGTH('a) - n mod LENGTH('a)) mod LENGTH('a)))\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4230
    using \<open>m < LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4231
    by (simp only: of_nat_mod mod_simps)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4232
      (simp add: of_nat_diff of_nat_mod Suc_le_eq add_less_mono algebra_simps mod_simps)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4233
  then have \<open>(m + n) mod LENGTH('a) =
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4234
    LENGTH('a) - Suc ((LENGTH('a) - Suc m + LENGTH('a) - n mod LENGTH('a)) mod LENGTH('a))\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4235
    by simp
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4236
  ultimately show \<open>bit (word_rotr n w) m \<longleftrightarrow> bit (word_reverse (word_rotl n (word_reverse w))) m\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4237
    by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff bit_word_reverse_iff)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4238
qed
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4239
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4240
lemma word_roti_0 [simp]: "word_roti 0 w = w"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4241
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4242
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4243
lemma word_roti_add: "word_roti (m + n) w = word_roti m (word_roti n w)"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4244
  by (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4245
    (simp add: bit_word_roti_iff nat_less_iff mod_simps ac_simps)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4246
67118
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4247
lemma word_roti_conv_mod':
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4248
  "word_roti n w = word_roti (n mod int (size w)) w"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4249
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4250
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4251
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4252
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4253
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4254
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4255
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4256
subsubsection \<open>"Word rotation commutes with bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4257
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  4258
\<comment> \<open>using locale to not pollute lemma namespace\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4259
locale word_rotate
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4260
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4261
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4262
context
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4263
  includes bit_operations_syntax
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4264
begin
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4265
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4266
lemma word_rot_logs:
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  4267
  "word_rotl n (NOT v) = NOT (word_rotl n v)"
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  4268
  "word_rotr n (NOT v) = NOT (word_rotr n v)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4269
  "word_rotl n (x AND y) = word_rotl n x AND word_rotl n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4270
  "word_rotr n (x AND y) = word_rotr n x AND word_rotr n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4271
  "word_rotl n (x OR y) = word_rotl n x OR word_rotl n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4272
  "word_rotr n (x OR y) = word_rotr n x OR word_rotr n y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4273
  "word_rotl n (x XOR y) = word_rotl n x XOR word_rotl n y"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4274
  "word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y"
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
  4275
  by (rule bit_word_eqI, auto simp add: bit_word_rotl_iff bit_word_rotr_iff bit_and_iff bit_or_iff bit_xor_iff bit_not_iff algebra_simps not_le)+
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4276
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4277
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4278
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4279
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4280
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4281
lemmas word_rot_logs = word_rotate.word_rot_logs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4282
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4283
lemma word_rotx_0 [simp] : "word_rotr i 0 = 0 \<and> word_rotl i 0 = 0"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4284
  by transfer simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4285
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4286
lemma word_roti_0' [simp] : "word_roti n 0 = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4287
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4288
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4289
declare word_roti_eq_word_rotr_word_rotl [simp]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4290
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4291
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4292
subsection \<open>Maximum machine word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4293
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4294
context
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4295
  includes bit_operations_syntax
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4296
begin
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4297
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4298
lemma word_int_cases:
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
  4299
  fixes x :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4300
  obtains n where "x = word_of_int n" and "0 \<le> n" and "n < 2^LENGTH('a)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4301
  by (rule that [of \<open>uint x\<close>]) simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4302
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4303
lemma word_nat_cases [cases type: word]:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4304
  fixes x :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4305
  obtains n where "x = of_nat n" and "n < 2^LENGTH('a)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4306
  by (rule that [of \<open>unat x\<close>]) simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4307
73788
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4308
lemma max_word_max [intro!]:
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4309
  \<open>n \<le> - 1\<close> for n :: \<open>'a::len word\<close>
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  4310
  by (fact word_order.extremum)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4311
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
  4312
lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)"
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4313
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4314
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4315
lemma word_pow_0: "(2::'a::len word) ^ LENGTH('a) = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  4316
  by (fact word_exp_length_eq_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4317
73788
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4318
lemma max_word_wrap: 
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4319
  \<open>x + 1 = 0 \<Longrightarrow> x = - 1\<close> for x :: \<open>'a::len word\<close>
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4320
  by (simp add: eq_neg_iff_add_eq_0)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4321
73788
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4322
lemma word_and_max:
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4323
  \<open>x AND - 1 = x\<close> for x :: \<open>'a::len word\<close>
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4324
  by (fact word_log_esimps)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4325
73788
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4326
lemma word_or_max:
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4327
  \<open>x OR - 1 = - 1\<close> for x :: \<open>'a::len word\<close>
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4328
  by (fact word_log_esimps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4329
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4330
lemma word_ao_dist2: "x AND (y OR z) = x AND y OR x AND z"
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
  4331
  for x y z :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  4332
  by (fact bit.conj_disj_distrib)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4333
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4334
lemma word_oa_dist2: "x OR y AND z = (x OR y) AND (x OR z)"
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
  4335
  for x y z :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  4336
  by (fact bit.disj_conj_distrib)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4337
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4338
lemma word_and_not [simp]: "x AND NOT x = 0"
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
  4339
  for x :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  4340
  by (fact bit.conj_cancel_right)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4341
73788
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4342
lemma word_or_not [simp]:
35217bf33215 max word moved to Word_Lib in AFP
haftmann
parents: 73682
diff changeset
  4343
  \<open>x OR NOT x = - 1\<close> for x :: \<open>'a::len word\<close>
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  4344
  by (fact bit.disj_cancel_right)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4345
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4346
lemma word_xor_and_or: "x XOR y = x AND NOT y OR NOT x AND y"
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
  4347
  for x y :: "'a::len word"
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72489
diff changeset
  4348
  by (fact bit.xor_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4349
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4350
lemma uint_lt_0 [simp]: "uint x < 0 = False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4351
  by (simp add: linorder_not_less)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4352
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4353
lemma word_less_1 [simp]: "x < 1 \<longleftrightarrow> x = 0"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4354
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4355
  by (simp add: word_less_nat_alt unat_0_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4356
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4357
lemma uint_plus_if_size:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4358
  "uint (x + y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4359
    (if uint x + uint y < 2^size x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4360
     then uint x + uint y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4361
     else uint x + uint y - 2^size x)"
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
  4362
  by (simp add: take_bit_eq_mod word_size uint_word_of_int_eq uint_plus_if')
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4363
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4364
lemma unat_plus_if_size:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4365
  "unat (x + y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4366
    (if unat x + unat y < 2^size x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4367
     then unat x + unat y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4368
     else unat x + unat y - 2^size x)"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4369
  for x y :: "'a::len word"
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
  4370
  by (simp add: size_word.rep_eq unat_arith_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4371
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4372
lemma word_neq_0_conv: "w \<noteq> 0 \<longleftrightarrow> 0 < w"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4373
  for w :: "'a::len word"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4374
  by (fact word_coorder.not_eq_extremum)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4375
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4376
lemma max_lt: "unat (max a b div c) = unat (max a b) div unat c"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4377
  for c :: "'a::len word"
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  4378
  by (fact unat_div)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4379
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4380
lemma uint_sub_if_size:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4381
  "uint (x - y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4382
    (if uint y \<le> uint x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4383
     then uint x - uint y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4384
     else uint x - uint y + 2^size x)"
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
  4385
  by (simp add: size_word.rep_eq uint_sub_if')
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4386
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4387
lemma unat_sub:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4388
  \<open>unat (a - b) = unat a - unat b\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4389
  if \<open>b \<le> a\<close>
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
  4390
  by (meson that unat_sub_if_size word_le_nat_alt)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4391
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4392
lemmas word_less_sub1_numberof [simp] = word_less_sub1 [of "numeral w"] for w
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4393
lemmas word_le_sub1_numberof [simp] = word_le_sub1 [of "numeral w"] for w
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4394
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4395
lemma word_of_int_minus: "word_of_int (2^LENGTH('a) - i) = (word_of_int (-i)::'a::len word)"
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
  4396
  by simp
72292
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4397
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4398
lemma word_of_int_inj:
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4399
  \<open>(word_of_int x :: 'a::len word) = word_of_int y \<longleftrightarrow> x = y\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4400
  if \<open>0 \<le> x \<and> x < 2 ^ LENGTH('a)\<close> \<open>0 \<le> y \<and> y < 2 ^ LENGTH('a)\<close>
4a58c38b85ff factored out typedef material
haftmann
parents: 72281
diff changeset
  4401
  using that by (transfer fixing: x y) (simp add: take_bit_int_eq_self) 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4402
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4403
lemma word_le_less_eq: "x \<le> y \<longleftrightarrow> x = y \<or> x < y"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4404
  for x y :: "'z::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4405
  by (auto simp add: order_class.le_less)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4406
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4407
lemma mod_plus_cong:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4408
  fixes b b' :: int
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4409
  assumes 1: "b = b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4410
    and 2: "x mod b' = x' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4411
    and 3: "y mod b' = y' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4412
    and 4: "x' + y' = z'"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4413
  shows "(x + y) mod b = z' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4414
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4415
  from 1 2[symmetric] 3[symmetric] have "(x + y) mod b = (x' mod b' + y' mod b') mod b'"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  4416
    by (simp add: mod_add_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4417
  also have "\<dots> = (x' + y') mod b'"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  4418
    by (simp add: mod_add_eq)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4419
  finally show ?thesis
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4420
    by (simp add: 4)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4421
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4422
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4423
lemma mod_minus_cong:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4424
  fixes b b' :: int
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4425
  assumes "b = b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4426
    and "x mod b' = x' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4427
    and "y mod b' = y' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4428
    and "x' - y' = z'"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4429
  shows "(x - y) mod b = z' mod b'"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4430
  using assms [symmetric] by (auto intro: mod_diff_cong)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4431
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
  4432
lemma word_induct_less [case_names zero less]:
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4433
  \<open>P m\<close> if zero: \<open>P 0\<close> and less: \<open>\<And>n. n < m \<Longrightarrow> P n \<Longrightarrow> P (1 + n)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4434
  for m :: \<open>'a::len word\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4435
proof -
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4436
  define q where \<open>q = unat m\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4437
  with less have \<open>\<And>n. n < word_of_nat q \<Longrightarrow> P n \<Longrightarrow> P (1 + n)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4438
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4439
  then have \<open>P (word_of_nat q :: 'a word)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4440
  proof (induction q)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4441
    case 0
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4442
    show ?case
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4443
      by (simp add: zero)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4444
  next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4445
    case (Suc q)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4446
    show ?case
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4447
    proof (cases \<open>1 + word_of_nat q = (0 :: 'a word)\<close>)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4448
      case True
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4449
      then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4450
        by (simp add: zero)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4451
    next
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4452
      case False
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4453
      then have *: \<open>word_of_nat q < (word_of_nat (Suc q) :: 'a word)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4454
        by (simp add: unatSuc word_less_nat_alt)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4455
      then have **: \<open>n < (1 + word_of_nat q :: 'a word) \<longleftrightarrow> n \<le> (word_of_nat q :: 'a word)\<close> for n
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4456
        by (metis (no_types, lifting) add.commute inc_le le_less_trans not_less of_nat_Suc)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4457
      have \<open>P (word_of_nat q)\<close>
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
  4458
        by (simp add: "**" Suc.IH Suc.prems)
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4459
      with * have \<open>P (1 + word_of_nat q)\<close>
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4460
        by (rule Suc.prems)
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4461
      then show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4462
        by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4463
    qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4464
  qed
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4465
  with \<open>q = unat m\<close> show ?thesis
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4466
    by simp
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4467
qed
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4468
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4469
lemma word_induct: "P 0 \<Longrightarrow> (\<And>n. P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4470
  for P :: "'a::len word \<Rightarrow> bool"
72262
a282abb07642 integrated generic conversions into word corpse
haftmann
parents: 72261
diff changeset
  4471
  by (rule word_induct_less)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4472
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
  4473
lemma word_induct2 [case_names zero suc, induct type]: "P 0 \<Longrightarrow> (\<And>n. 1 + n \<noteq> 0 \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P n"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4474
  for P :: "'b::len word \<Rightarrow> bool"
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
  4475
by (induction rule: word_induct_less; force)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4476
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  4477
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4478
subsection \<open>Recursion combinator for words\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  4479
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
  4480
definition word_rec :: "'a \<Rightarrow> ('b::len word \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> 'b word \<Rightarrow> 'a"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4481
  where "word_rec forZero forSuc n = rec_nat forZero (forSuc \<circ> of_nat) (unat n)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4482
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
  4483
lemma word_rec_0 [simp]: "word_rec z s 0 = z"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4484
  by (simp add: word_rec_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4485
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
  4486
lemma word_rec_Suc [simp]: "1 + n \<noteq> 0 \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4487
  for n :: "'a::len word"
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
  4488
  by (simp add: unatSuc word_rec_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4489
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4490
lemma word_rec_Pred: "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))"
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
  4491
  by (metis add.commute diff_add_cancel word_rec_Suc)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4492
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4493
lemma word_rec_in: "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n"
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  4494
  by (induct n) simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4495
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  4496
lemma word_rec_in2: "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> (+) 1) n"
74101
d804e93ae9ff moved theory Bit_Operations into Main corpus
haftmann
parents: 74097
diff changeset
  4497
  by (induct n) simp_all
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
  4498
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4499
lemma word_rec_twice:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  4500
  "m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> (+) (n - m)) m"
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
  4501
proof (induction n arbitrary: z f)
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
  4502
  case zero
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
  4503
  then show ?case
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
  4504
    by (metis diff_0_right word_le_0_iff word_rec_0)
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
  4505
next
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
  4506
  case (suc n z f)
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
  4507
  show ?case
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
  4508
  proof (cases "1 + (n - m) = 0")
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
  4509
    case True
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
  4510
    then show ?thesis
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
  4511
      by (simp add: add_diff_eq)
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
  4512
  next
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
  4513
    case False
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
  4514
    then have eq: "1 + n - m = 1 + (n - m)"
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
  4515
      by simp
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
  4516
    with False have "m \<le> 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
  4517
      by (metis "suc.prems" add.commute dual_order.antisym eq_iff_diff_eq_0 inc_le leI)
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
  4518
    with False "suc.hyps" show ?thesis
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
  4519
      using suc.IH [of "f 0 z" "f \<circ> (+) 1"] 
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
  4520
      by (simp add: word_rec_in2 eq add.assoc o_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
  4521
  qed
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
  4522
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4523
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4524
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z"
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
  4525
  by (induct n) auto
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
  4526
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
  4527
lemma word_rec_id_eq: "(\<And>m. m < n \<Longrightarrow> f m = id) \<Longrightarrow> word_rec z f n = z"
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
  4528
  by (induction n) (auto simp add: unatSuc unat_arith_simps(2))
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4529
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4530
lemma word_rec_max:
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
  4531
  assumes "\<forall>m\<ge>n. m \<noteq> - 1 \<longrightarrow> f m = id"
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
  4532
  shows "word_rec z f (- 1) = word_rec z f 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
  4533
proof -
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
  4534
  have \<section>: "\<And>m. \<lbrakk>m < - 1 - n\<rbrakk> \<Longrightarrow> (f \<circ> (+) n) m = id"
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
  4535
    using assms
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
  4536
    by (metis (mono_tags, lifting) add.commute add_diff_cancel_left' comp_apply less_le olen_add_eqv plus_minus_no_overflow word_n1_ge)
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
  4537
  have "word_rec z f (- 1) = word_rec (word_rec z f (- 1 - (- 1 - n))) (f \<circ> (+) (- 1 - (- 1 - n))) (- 1 - 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
  4538
    by (meson word_n1_ge word_rec_twice)
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
  4539
  also have "... = word_rec z f 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
  4540
    by (metis (no_types, lifting) \<section> diff_add_cancel minus_diff_eq uminus_add_conv_diff word_rec_id_eq)
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
  4541
  finally show ?thesis .
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
  4542
qed
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4543
74097
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4544
end
6d7be1227d02 organize syntax for word operations in bundles
haftmann
parents: 73932
diff changeset
  4545
72512
83b5911c0164 more lemmas
haftmann
parents: 72508
diff changeset
  4546
74592
3c587b7c3d5c more generic bit/word lemmas for distribution
haftmann
parents: 74498
diff changeset
  4547
subsection \<open>Tool support\<close>
72489
a1366ce41368 early and more complete setup of tools
haftmann
parents: 72488
diff changeset
  4548
69605
a96320074298 isabelle update -u path_cartouches;
wenzelm
parents: 69064
diff changeset
  4549
ML_file \<open>Tools/smt_word.ML\<close>
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 35049
diff changeset
  4550
41060
4199fdcfa3c0 moved smt_word.ML into the directory of the Word library
boehmes
parents: 40827
diff changeset
  4551
end