src/HOL/Word/Word.thy
author haftmann
Thu, 17 Sep 2020 09:57:30 +0000
changeset 72261 5193570b739a
parent 72244 4b011fa5e83b
child 72262 a282abb07642
permissions -rw-r--r--
more lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29628
d9294387ab0e entry point for Word library now named Word
haftmann
parents: 27137
diff changeset
     1
(*  Title:      HOL/Word/Word.thy
46124
3ee75fe01986 misc tuning;
wenzelm
parents: 46064
diff changeset
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
*)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
     5
section \<open>A type of finite bit strings\<close>
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     6
29628
d9294387ab0e entry point for Word library now named Word
haftmann
parents: 27137
diff changeset
     7
theory Word
41413
64cd30d6b0b8 explicit file specifications -- avoid secondary load path;
wenzelm
parents: 41060
diff changeset
     8
imports
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65363
diff changeset
     9
  "HOL-Library.Type_Length"
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 65363
diff changeset
    10
  "HOL-Library.Boolean_Algebra"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
    11
  "HOL-Library.Bit_Operations"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70187
diff changeset
    12
  Bits_Int
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
    13
  Traditional_Syntax
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
    14
  Bit_Comprehension
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 51717
diff changeset
    15
  Misc_Typedef
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    16
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    17
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    18
subsection \<open>Preliminaries\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    19
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    20
lemma signed_take_bit_decr_length_iff:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    21
  \<open>signed_take_bit (LENGTH('a::len) - Suc 0) k = signed_take_bit (LENGTH('a) - Suc 0) l
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    22
    \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    23
  by (cases \<open>LENGTH('a)\<close>)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    24
    (simp_all add: signed_take_bit_eq_iff_take_bit_eq)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    25
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    26
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    27
subsection \<open>Fundamentals\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    28
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    29
subsubsection \<open>Type definition\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    30
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    31
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    32
  morphisms rep Word by (auto intro!: equivpI reflpI sympI transpI)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    33
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    34
hide_const (open) rep \<comment> \<open>only for foundational purpose\<close>
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
    35
hide_const (open) Word \<comment> \<open>only for code generation\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
    36
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    37
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    38
subsubsection \<open>Basic arithmetic\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    39
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    40
instantiation word :: (len) comm_ring_1
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    41
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    42
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    43
lift_definition zero_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    44
  is 0 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    45
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    46
lift_definition one_word :: \<open>'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    47
  is 1 .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    48
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    49
lift_definition plus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    50
  is \<open>(+)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    51
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    52
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    53
lift_definition minus_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    54
  is \<open>(-)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    55
  by (auto simp add: take_bit_eq_mod intro: mod_diff_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    56
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    57
lift_definition uminus_word :: \<open>'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    58
  is uminus
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    59
  by (auto simp add: take_bit_eq_mod intro: mod_minus_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    60
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    61
lift_definition times_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    62
  is \<open>(*)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    63
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    64
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    65
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    66
  by (standard; transfer) (simp_all add: algebra_simps)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    67
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    68
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    69
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    70
context
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    71
  includes lifting_syntax
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    72
  notes
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    73
    power_transfer [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    74
    transfer_rule_of_bool [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    75
    transfer_rule_numeral [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    76
    transfer_rule_of_nat [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    77
    transfer_rule_of_int [transfer_rule]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    78
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    79
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    80
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    81
lemma power_transfer_word [transfer_rule]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    82
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    83
  by transfer_prover
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
    84
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    85
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    86
  \<open>((=) ===> pcr_word) of_bool of_bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    87
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    88
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    89
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    90
  \<open>((=) ===> pcr_word) numeral numeral\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    91
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    92
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    93
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    94
  \<open>((=) ===> pcr_word) int of_nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    95
  by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    96
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    97
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    98
  \<open>((=) ===> pcr_word) (\<lambda>k. k) of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
    99
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   100
  have \<open>((=) ===> pcr_word) of_int of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   101
    by transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   102
  then show ?thesis by (simp add: id_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   103
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   104
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   105
lemma [transfer_rule]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   106
  \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   107
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   108
  have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q")
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   109
    for k :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   110
  proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   111
    assume ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   112
    then show ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   113
      by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   114
  next
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   115
    assume ?Q
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   116
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   117
    then have "even (take_bit LENGTH('a) k)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   118
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   119
    then show ?P
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   120
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   121
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   122
  show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   123
    transfer_prover
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   124
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   125
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   126
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   127
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   128
lemma word_exp_length_eq_0 [simp]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   129
  \<open>(2 :: 'a::len word) ^ LENGTH('a) = 0\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   130
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   131
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   132
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   133
subsubsection \<open>Basic code generation setup\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   134
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
   135
lift_definition uint :: \<open>'a::len word \<Rightarrow> int\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   136
  is \<open>take_bit LENGTH('a)\<close> .
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   137
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   138
lemma [code abstype]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   139
  \<open>Word.Word (uint w) = w\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   140
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   141
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   142
quickcheck_generator word
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   143
  constructors:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   144
    \<open>0 :: 'a::len word\<close>,
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   145
    \<open>numeral :: num \<Rightarrow> 'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   146
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   147
instantiation word :: (len) equal
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   148
begin
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   149
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   150
lift_definition equal_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> bool\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   151
  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
   152
  by simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   153
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   154
instance
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   155
  by (standard; transfer) rule
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   156
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   157
end
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   158
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   159
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   160
  \<open>HOL.equal v w \<longleftrightarrow> HOL.equal (uint v) (uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   161
  by transfer (simp add: equal)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   162
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   163
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   164
  \<open>uint 0 = 0\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   165
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   166
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   167
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   168
  \<open>uint 1 = 1\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   169
  by transfer simp
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   170
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   171
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   172
  \<open>uint (v + w) = take_bit LENGTH('a) (uint v + uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   173
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   174
  by transfer (simp add: take_bit_add)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   175
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   176
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   177
  \<open>uint (- w) = (let k = uint w in if w = 0 then 0 else 2 ^ LENGTH('a) - k)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   178
  for w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   179
  by transfer (auto simp add: take_bit_eq_mod zmod_zminus1_eq_if)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   180
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   181
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   182
  \<open>uint (v - w) = take_bit LENGTH('a) (uint v - uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   183
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   184
  by transfer (simp add: take_bit_diff)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   185
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   186
lemma [code]:
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   187
  \<open>uint (v * w) = take_bit LENGTH('a) (uint v * uint w)\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   188
  for v w :: \<open>'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   189
  by transfer (simp add: take_bit_mult)
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   190
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   191
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   192
subsubsection \<open>Basic conversions\<close>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   193
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   194
lift_definition word_of_int :: \<open>int \<Rightarrow> 'a::len word\<close>
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   195
  is \<open>\<lambda>k. k\<close> .
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   196
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   197
lift_definition unat :: \<open>'a::len word \<Rightarrow> nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   198
  is \<open>nat \<circ> take_bit LENGTH('a)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   199
  by simp
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   200
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   201
lift_definition sint :: \<open>'a::len word \<Rightarrow> int\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   202
  \<comment> \<open>treats the most-significant bit as a sign bit\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   203
  is \<open>signed_take_bit (LENGTH('a) - 1)\<close>  
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   204
  by (simp add: signed_take_bit_decr_length_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   205
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   206
lemma nat_uint_eq [simp]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   207
  \<open>nat (uint w) = unat w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   208
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   209
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   210
lemma of_nat_word_eq_iff:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   211
  \<open>of_nat m = (of_nat n :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) m = take_bit LENGTH('a) n\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   212
  by transfer (simp add: take_bit_of_nat)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   213
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   214
lemma of_nat_word_eq_0_iff:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   215
  \<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   216
  using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   217
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   218
lemma of_int_word_eq_iff:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   219
  \<open>of_int k = (of_int l :: 'a::len word) \<longleftrightarrow> take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
72243
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   220
  by transfer rule
eaac77208cf9 tuned theory structure
haftmann
parents: 72242
diff changeset
   221
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   222
lemma of_int_word_eq_0_iff:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   223
  \<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   224
  using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   225
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   226
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   227
subsubsection \<open>Basic ordering\<close>
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   228
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
   229
instantiation word :: (len) linorder
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   230
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   231
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   232
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   233
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   234
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   235
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   236
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   237
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   238
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   239
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   240
instance
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   241
  by (standard; transfer) auto
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   242
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   243
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   244
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   245
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
   246
  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
   247
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   248
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
   249
  by (standard; transfer) simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   250
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   251
lemma word_le_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   252
  "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   253
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   254
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   255
lemma word_less_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   256
  "a < b \<longleftrightarrow> uint a < uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   257
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   258
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   259
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
   260
  \<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
   261
  by transfer (simp add: less_le)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   262
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   263
lemma of_nat_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   264
  \<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
   265
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   266
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   267
lemma of_nat_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   268
  \<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
   269
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   270
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   271
lemma of_int_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   272
  \<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
   273
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   274
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   275
lemma of_int_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   276
  \<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
   277
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   278
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   279
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   280
subsection \<open>Bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   281
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   282
instantiation word :: (len) semiring_modulo
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   283
begin
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   284
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   285
lift_definition divide_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   286
  is \<open>\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   287
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   288
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   289
lift_definition modulo_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   290
  is \<open>\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   291
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   292
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   293
instance proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   294
  show "a div b * b + a mod b = a" for a b :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   295
  proof transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   296
    fix k l :: int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   297
    define r :: int where "r = 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   298
    then have r: "take_bit LENGTH('a) k = k mod r" for k
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   299
      by (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   300
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   301
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   302
      by (simp add: div_mult_mod_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   303
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   304
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   305
      by (simp add: mod_add_left_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   306
    also have "... = (((k mod r) div (l mod r) * l) mod r
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   307
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   308
      by (simp add: mod_mult_right_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   309
    finally have "k mod r = ((k mod r) div (l mod r) * l
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   310
      + (k mod r) mod (l mod r)) mod r"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   311
      by (simp add: mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   312
    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
   313
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   314
      by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   315
  qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   316
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   317
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   318
end
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   319
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   320
instance word :: (len) semiring_parity
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   321
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   322
  show "\<not> 2 dvd (1::'a word)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   323
    by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   324
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   325
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   326
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   327
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   328
    for a :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   329
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   330
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   331
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   332
lemma word_bit_induct [case_names zero even odd]:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   333
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   334
    and word_even: \<open>\<And>a. P a \<Longrightarrow> 0 < a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   335
    and word_odd: \<open>\<And>a. P a \<Longrightarrow> a < 2 ^ (LENGTH('a) - 1) \<Longrightarrow> P (1 + 2 * a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   336
  for P and a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   337
proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   338
  define m :: nat where \<open>m = LENGTH('a) - 1\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   339
  then have l: \<open>LENGTH('a) = Suc m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   340
    by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   341
  define n :: nat where \<open>n = unat a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   342
  then have \<open>n < 2 ^ LENGTH('a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   343
    by (unfold unat_def) (transfer, simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   344
  then have \<open>n < 2 * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   345
    by (simp add: l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   346
  then have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   347
  proof (induction n rule: nat_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   348
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   349
    show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   350
      by simp (rule word_zero)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   351
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   352
    case (even n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   353
    then have \<open>n < 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   354
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   355
    with even.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   356
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   357
    moreover from \<open>n < 2 ^ m\<close> even.hyps have \<open>0 < (of_nat n :: 'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   358
      by (auto simp add: word_greater_zero_iff of_nat_word_eq_0_iff l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   359
    moreover from \<open>n < 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   360
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   361
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   362
    ultimately have \<open>P (2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   363
      by (rule word_even)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   364
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   365
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   366
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   367
    case (odd n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   368
    then have \<open>Suc n \<le> 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   369
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   370
    with odd.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   371
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   372
    moreover from \<open>Suc n \<le> 2 ^ m\<close> have \<open>(of_nat n :: 'a word) < 2 ^ (LENGTH('a) - 1)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   373
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
   374
      by (simp add: l take_bit_eq_mod)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   375
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   376
      by (rule word_odd)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   377
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   378
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   379
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   380
  moreover have \<open>of_nat (nat (uint a)) = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   381
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   382
  ultimately show ?thesis
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   383
    by (simp add: n_def)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   384
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   385
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   386
lemma bit_word_half_eq:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   387
  \<open>(of_bool b + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   388
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   389
    for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   390
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   391
  case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   392
  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
   393
    by auto
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   394
  with False that show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   395
    by transfer (simp add: eq_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   396
next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   397
  case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   398
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   399
    by (cases \<open>LENGTH('a)\<close>) simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   400
  show ?thesis proof (cases b)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   401
    case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   402
    moreover have \<open>a * 2 div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   403
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   404
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   405
      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
   406
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   407
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   408
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   409
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   410
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   411
      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
   412
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   413
      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
   414
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   415
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   416
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   417
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   418
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   419
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   420
    case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   421
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   422
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   423
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   424
      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
   425
        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
   426
      moreover assume \<open>take_bit LENGTH('a) k < take_bit LENGTH('a) (2 ^ (LENGTH('a) - Suc 0))\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   427
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   428
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   429
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   430
      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
   431
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   432
      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
   433
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   434
        by (auto simp add: take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   435
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   436
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   437
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   438
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   439
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   440
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   441
lemma even_mult_exp_div_word_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   442
  \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> (
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   443
    m \<le> n \<and>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   444
    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
   445
  by transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   446
    (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
   447
      simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   448
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   449
instantiation word :: (len) semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   450
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   451
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   452
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
   453
  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
   454
proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   455
  fix k l :: int and n :: nat
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   456
  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
   457
  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
   458
  proof (cases \<open>n < LENGTH('a)\<close>)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   459
    case True
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   460
    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
   461
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   462
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   463
      by (simp add: bit_take_bit_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   464
  next
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   465
    case False
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   466
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   467
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   468
  qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   469
qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   470
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   471
instance proof
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   472
  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
   473
    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
   474
  for P and a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   475
  proof (induction a rule: word_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   476
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   477
    have \<open>0 div 2 = (0::'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   478
      by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   479
    with stable [of 0] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   480
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   481
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   482
    case (even a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   483
    with rec [of a False] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   484
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   485
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   486
    case (odd a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   487
    with rec [of a True] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   488
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   489
  qed
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   490
  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
   491
    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
   492
  show \<open>0 div a = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   493
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   494
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   495
  show \<open>a div 1 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   496
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   497
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   498
  show \<open>a mod b div b = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   499
    for a b :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   500
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   501
    apply (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   502
    apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>])
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   503
      apply simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   504
     apply (metis le_less mod_by_0 pos_mod_conj zero_less_numeral zero_less_power)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   505
    using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   506
  proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   507
    fix aa :: int and ba :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   508
    have f1: "\<And>i n. (i::int) mod 2 ^ n = 0 \<or> 0 < i mod 2 ^ n"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   509
      by (metis le_less take_bit_eq_mod take_bit_nonnegative)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   510
    have "(0::int) < 2 ^ len_of (TYPE('a)::'a itself) \<and> ba mod 2 ^ len_of (TYPE('a)::'a itself) \<noteq> 0 \<or> aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   511
      by (metis (no_types) mod_by_0 unique_euclidean_semiring_numeral_class.pos_mod_bound zero_less_numeral zero_less_power)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   512
    then show "aa mod 2 ^ len_of (TYPE('a)::'a itself) mod (ba mod 2 ^ len_of (TYPE('a)::'a itself)) < 2 ^ len_of (TYPE('a)::'a itself)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   513
      using f1 by (meson le_less less_le_trans unique_euclidean_semiring_numeral_class.pos_mod_bound)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   514
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   515
  show \<open>(1 + a) div 2 = a div 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   516
    if \<open>even a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   517
    for a :: \<open>'a word\<close>
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   518
    using that by transfer
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   519
      (auto dest: le_Suc_ex simp add: mod_2_eq_odd take_bit_Suc elim!: evenE)
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   520
  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
   521
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   522
    by transfer (simp, simp add: exp_div_exp_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   523
  show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   524
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   525
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   526
    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
   527
    apply (simp add: drop_bit_take_bit)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   528
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   529
  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
   530
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   531
    by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   532
  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
   533
    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
   534
    using that apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   535
    apply (auto simp flip: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   536
           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
   537
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   538
  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
   539
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   540
    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
   541
  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
   542
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   543
    by transfer (auto simp add: take_bit_of_mask even_mask_div_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   544
  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
   545
    for a :: \<open>'a word\<close> and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   546
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   547
    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
   548
      n < m
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   549
      \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   550
      \<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
   551
    for m n :: nat and k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   552
      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
   553
        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
   554
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   555
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   556
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   557
end
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   558
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   559
instantiation word :: (len) semiring_bit_shifts
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   560
begin
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   561
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   562
lift_definition push_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   563
  is push_bit
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   564
proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   565
  show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   566
    if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   567
  proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   568
    from that
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   569
    have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   570
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   571
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   572
    moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   573
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   574
    ultimately show ?thesis
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   575
      by (simp add: take_bit_push_bit)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   576
  qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   577
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   578
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   579
lift_definition drop_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   580
  is \<open>\<lambda>n. drop_bit n \<circ> take_bit LENGTH('a)\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   581
  by (simp add: take_bit_eq_mod)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   582
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   583
lift_definition take_bit_word :: \<open>nat \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   584
  is \<open>\<lambda>n. take_bit (min LENGTH('a) n)\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   585
  by (simp add: ac_simps) (simp only: flip: take_bit_take_bit)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   586
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   587
instance proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   588
  show \<open>push_bit n a = a * 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   589
    by transfer (simp add: push_bit_eq_mult)
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   590
  show \<open>drop_bit n a = a div 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   591
    by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit)
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   592
  show \<open>take_bit n a = a mod 2 ^ n\<close> for n :: nat and a :: \<open>'a word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   593
    by transfer (auto simp flip: take_bit_eq_mod)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   594
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   595
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   596
end
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   597
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   598
lemma bit_word_eqI:
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
   599
  \<open>a = b\<close> if \<open>\<And>n. n < LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   600
  for a b :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   601
  using that by transfer (auto simp add: nat_less_le bit_eq_iff bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   602
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   603
lemma bit_imp_le_length:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   604
  \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   605
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   606
  using that by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   607
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   608
lemma not_bit_length [simp]:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   609
  \<open>\<not> bit w LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   610
  by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   611
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   612
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   613
subsection \<open>Conversions including casts\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   614
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   615
lemma uint_nonnegative: "0 \<le> uint w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   616
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   617
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   618
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   619
  for w :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   620
  by transfer (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   621
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   622
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   623
  for w :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   624
  using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   625
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   626
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   627
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   628
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   629
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   630
  using word_uint_eqI by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   631
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   632
lemma Word_eq_word_of_int [code_post]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   633
  \<open>Word.Word = word_of_int\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   634
  by rule (transfer, rule)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   635
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   636
lemma uint_word_of_int_eq [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   637
  \<open>uint (word_of_int k :: 'a::len word) = take_bit LENGTH('a) k\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   638
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   639
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   640
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
   641
  by (simp add: uint_word_of_int_eq take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   642
  
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   643
lemma word_of_int_uint: "word_of_int (uint w) = w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   644
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   645
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   646
lemma word_div_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   647
  "a div b = word_of_int (uint a div uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   648
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   649
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   650
lemma word_mod_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   651
  "a mod b = word_of_int (uint a mod uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   652
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   653
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   654
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
   655
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   656
  fix x :: "'a word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   657
  assume "\<And>x. PROP P (word_of_int x)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   658
  then have "PROP P (word_of_int (uint x))" .
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   659
  then show "PROP P x" by (simp add: word_of_int_uint)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   660
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   661
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   662
lemma sint_uint [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   663
  \<open>sint w = signed_take_bit (LENGTH('a) - 1) (uint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   664
  for w :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   665
  by (cases \<open>LENGTH('a)\<close>; transfer) (simp_all add: signed_take_bit_take_bit)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   666
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   667
lemma unat_eq_nat_uint [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   668
  \<open>unat w = nat (uint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   669
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   670
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   671
lift_definition ucast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   672
  is \<open>take_bit LENGTH('a)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   673
  by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   674
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   675
lemma ucast_eq [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   676
  \<open>ucast w = word_of_int (uint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   677
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   678
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   679
lift_definition scast :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   680
  is \<open>signed_take_bit (LENGTH('a) - 1)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   681
  by (simp flip: signed_take_bit_decr_length_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   682
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   683
lemma scast_eq [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   684
  \<open>scast w = word_of_int (sint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   685
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   686
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   687
lemma uint_0_eq [simp]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   688
  \<open>uint 0 = 0\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   689
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   690
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   691
lemma uint_1_eq [simp]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   692
  \<open>uint 1 = 1\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   693
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   694
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   695
lemma word_m1_wi: "- 1 = word_of_int (- 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   696
  by transfer rule
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   697
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   698
lemma uint_0_iff: "uint x = 0 \<longleftrightarrow> x = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   699
  by (simp add: word_uint_eq_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   700
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   701
lemma unat_0_iff: "unat x = 0 \<longleftrightarrow> x = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   702
  by transfer (auto intro: antisym)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   703
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   704
lemma unat_0 [simp]: "unat 0 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   705
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   706
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   707
lemma unat_gt_0: "0 < unat x \<longleftrightarrow> x \<noteq> 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   708
  by (auto simp: unat_0_iff [symmetric])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   709
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   710
lemma ucast_0 [simp]: "ucast 0 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   711
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   712
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   713
lemma sint_0 [simp]: "sint 0 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   714
  by (simp add: sint_uint)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   715
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   716
lemma scast_0 [simp]: "scast 0 = 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   717
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   718
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   719
lemma sint_n1 [simp] : "sint (- 1) = - 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   720
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   721
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   722
lemma scast_n1 [simp]: "scast (- 1) = - 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   723
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   724
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   725
lemma uint_1: "uint (1::'a::len word) = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   726
  by (fact uint_1_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   727
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   728
lemma unat_1 [simp]: "unat (1::'a::len word) = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   729
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   730
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   731
lemma ucast_1 [simp]: "ucast (1::'a::len word) = 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   732
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   733
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   734
instantiation word :: (len) size
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   735
begin
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   736
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   737
lift_definition size_word :: \<open>'a word \<Rightarrow> nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   738
  is \<open>\<lambda>_. LENGTH('a)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   739
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   740
instance ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   741
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   742
end
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   743
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   744
lemma word_size [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   745
  \<open>size w = LENGTH('a)\<close> for w :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   746
  by (fact size_word.rep_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   747
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   748
lemma word_size_gt_0 [iff]: "0 < size w"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   749
  for w :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   750
  by (simp add: word_size)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   751
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   752
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   753
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   754
lemma lens_not_0 [iff]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   755
  \<open>size w \<noteq> 0\<close> for  w :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   756
  by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   757
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   758
lift_definition source_size :: \<open>('a::len word \<Rightarrow> 'b) \<Rightarrow> nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   759
  is \<open>\<lambda>_. LENGTH('a)\<close> .
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   760
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   761
lift_definition target_size :: \<open>('a \<Rightarrow> 'b::len word) \<Rightarrow> nat\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   762
  is \<open>\<lambda>_. LENGTH('b)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   763
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   764
lift_definition is_up :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   765
  is \<open>\<lambda>_. LENGTH('a) \<le> LENGTH('b)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   766
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   767
lift_definition is_down :: \<open>('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   768
  is \<open>\<lambda>_. LENGTH('a) \<ge> LENGTH('b)\<close> ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   769
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   770
lemma is_up_eq:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   771
  \<open>is_up f \<longleftrightarrow> source_size f \<le> target_size f\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   772
  for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   773
  by (simp add: source_size.rep_eq target_size.rep_eq is_up.rep_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   774
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   775
lemma is_down_eq:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   776
  \<open>is_down f \<longleftrightarrow> target_size f \<le> source_size f\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   777
  for f :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   778
  by (simp add: source_size.rep_eq target_size.rep_eq is_down.rep_eq)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   779
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   780
lift_definition word_int_case :: \<open>(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   781
  is \<open>\<lambda>f. f \<circ> take_bit LENGTH('a)\<close> by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   782
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   783
lemma word_int_case_eq_uint [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   784
  \<open>word_int_case f w = f (uint w)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   785
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   786
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   787
translations
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   788
  "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
   789
  "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
   790
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   791
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   792
subsection \<open>Arithmetic operations\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   793
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   794
text \<open>Legacy theorems:\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   795
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   796
lemma word_add_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   797
  "a + b = word_of_int (uint a + uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   798
  by transfer (simp add: take_bit_add)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   799
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   800
lemma word_sub_wi [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   801
  "a - b = word_of_int (uint a - uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   802
  by transfer (simp add: take_bit_diff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   803
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   804
lemma word_mult_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   805
  "a * b = word_of_int (uint a * uint b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   806
  by transfer (simp add: take_bit_eq_mod mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   807
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   808
lemma word_minus_def [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   809
  "- a = word_of_int (- uint a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   810
  by transfer (simp add: take_bit_minus)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   811
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   812
lemma word_0_wi:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   813
  "0 = word_of_int 0"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   814
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   815
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   816
lemma word_1_wi:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   817
  "1 = word_of_int 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   818
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   819
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   820
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   821
  by (auto simp add: take_bit_eq_mod intro: mod_add_cong)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   822
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   823
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   824
  by (auto simp add: take_bit_eq_mod intro: mod_diff_cong)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   825
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   826
lemma word_succ_alt [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   827
  "word_succ a = word_of_int (uint a + 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   828
  by transfer (simp add: take_bit_eq_mod mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   829
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   830
lemma word_pred_alt [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   831
  "word_pred a = word_of_int (uint a - 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   832
  by transfer (simp add: take_bit_eq_mod mod_simps)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   833
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   834
lemmas word_arith_wis = 
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   835
  word_add_def word_sub_wi word_mult_def
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   836
  word_minus_def word_succ_alt word_pred_alt
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   837
  word_0_wi word_1_wi
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   838
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   839
lemma wi_homs:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   840
  shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   841
    and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   842
    and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   843
    and wi_hom_neg: "- word_of_int a = word_of_int (- a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   844
    and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   845
    and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   846
  by (transfer, simp)+
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   847
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   848
lemmas wi_hom_syms = wi_homs [symmetric]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   849
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   850
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   851
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   852
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   853
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   854
lemma word_of_nat: "of_nat n = word_of_int (int n)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   855
  by (induct n) (auto simp add : word_of_int_hom_syms)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   856
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   857
lemma word_of_int: "of_int = word_of_int"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   858
  apply (rule ext)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   859
  apply (case_tac x rule: int_diff_cases)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   860
  apply (simp add: word_of_nat wi_hom_sub)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   861
  done
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   862
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   863
lemma word_of_int_eq:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   864
  "word_of_int = of_int"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   865
  by (rule ext) (transfer, rule)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   866
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   867
definition udvd :: "'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool" (infixl "udvd" 50)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   868
  where "a udvd b = (\<exists>n\<ge>0. uint b = n * uint a)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   869
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   870
lemma exp_eq_zero_iff:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   871
  \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   872
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   873
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   874
lemma double_eq_zero_iff:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   875
  \<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   876
  for a :: \<open>'a::len word\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   877
proof -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   878
  define n where \<open>n = LENGTH('a) - Suc 0\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   879
  then have *: \<open>LENGTH('a) = Suc n\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   880
    by simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   881
  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
   882
    using that by transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   883
      (auto simp add: take_bit_eq_0_iff take_bit_eq_mod *)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   884
  moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   885
    by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   886
  then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   887
    by (simp add: *)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   888
  ultimately show ?thesis
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   889
    by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   890
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   891
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   892
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   893
subsection \<open>Ordering\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   894
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   895
lift_definition word_sle :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close>  (\<open>(_/ <=s _)\<close> [50, 51] 50)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   896
  is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - 1) k \<le> signed_take_bit (LENGTH('a) - 1) l\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   897
  by (simp flip: signed_take_bit_decr_length_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   898
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   899
lemma word_sle_eq [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   900
  \<open>a <=s b \<longleftrightarrow> sint a \<le> sint b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   901
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   902
  
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   903
lift_definition word_sless :: \<open>'a::len word \<Rightarrow> 'a word \<Rightarrow> bool\<close>  (\<open>(_/ <s _)\<close> [50, 51] 50)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   904
  is \<open>\<lambda>k l. signed_take_bit (LENGTH('a) - 1) k < signed_take_bit (LENGTH('a) - 1) l\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   905
  by (simp flip: signed_take_bit_decr_length_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   906
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   907
lemma word_sless_eq:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   908
  \<open>x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   909
  by transfer (simp add: signed_take_bit_decr_length_iff less_le)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   910
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   911
lemma [code]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   912
  \<open>a <s b \<longleftrightarrow> sint a < sint b\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   913
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   914
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   915
lemma word_less_alt: "a < b \<longleftrightarrow> uint a < uint b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   916
  by (fact word_less_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   917
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   918
lemma signed_linorder: "class.linorder word_sle word_sless"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   919
  by (standard; transfer) (auto simp add: signed_take_bit_decr_length_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   920
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   921
interpretation signed: linorder "word_sle" "word_sless"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   922
  by (rule signed_linorder)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   923
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   924
lemma word_zero_le [simp]: "0 \<le> y"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   925
  for y :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   926
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   927
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   928
lemma word_m1_ge [simp] : "word_pred 0 \<ge> y" (* FIXME: delete *)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   929
  by transfer (simp add: take_bit_minus_one_eq_mask mask_eq_exp_minus_1 bintr_lt2p)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   930
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   931
lemma word_n1_ge [simp]: "y \<le> -1"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   932
  for y :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   933
  by (fact word_order.extremum)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   934
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   935
lemmas word_not_simps [simp] =
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   936
  word_zero_le [THEN leD] word_m1_ge [THEN leD] word_n1_ge [THEN leD]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   937
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   938
lemma word_gt_0: "0 < y \<longleftrightarrow> 0 \<noteq> y"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   939
  for y :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   940
  by (simp add: less_le)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   941
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   942
lemmas word_gt_0_no [simp] = word_gt_0 [of "numeral y"] for y
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   943
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   944
lemma word_sless_alt: "a <s b \<longleftrightarrow> sint a < sint b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   945
  by transfer simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   946
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   947
lemma word_le_nat_alt: "a \<le> b \<longleftrightarrow> unat a \<le> unat b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   948
  by transfer (simp add: nat_le_eq_zle)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   949
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   950
lemma word_less_nat_alt: "a < b \<longleftrightarrow> unat a < unat b"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   951
  by transfer (auto simp add: less_le [of 0])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   952
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   953
lemmas unat_mono = word_less_nat_alt [THEN iffD1]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   954
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   955
instance word :: (len) wellorder
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   956
proof
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   957
  fix P :: "'a word \<Rightarrow> bool" and a
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   958
  assume *: "(\<And>b. (\<And>a. a < b \<Longrightarrow> P a) \<Longrightarrow> P b)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   959
  have "wf (measure unat)" ..
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   960
  moreover have "{(a, b :: ('a::len) word). a < b} \<subseteq> measure unat"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   961
    by (auto simp add: word_less_nat_alt)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   962
  ultimately have "wf {(a, b :: ('a::len) word). a < b}"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   963
    by (rule wf_subset)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   964
  then show "P a" using *
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   965
    by induction blast
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   966
qed
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   967
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   968
lemma wi_less:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   969
  "(word_of_int n < (word_of_int m :: 'a::len word)) =
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   970
    (n mod 2 ^ LENGTH('a) < m mod 2 ^ LENGTH('a))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   971
  by transfer (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   972
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   973
lemma wi_le:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   974
  "(word_of_int n \<le> (word_of_int m :: 'a::len word)) =
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   975
    (n mod 2 ^ LENGTH('a) \<le> m mod 2 ^ LENGTH('a))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   976
  by transfer (simp add: take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   977
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   978
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   979
subsection \<open>Bit-wise operations\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   980
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
   981
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   982
lemma uint_take_bit_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   983
  \<open>uint (take_bit n w) = take_bit n (uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   984
  by transfer (simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
   985
72227
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
   986
lemma take_bit_word_eq_self:
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
   987
  \<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
   988
  using that by transfer simp
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
   989
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
   990
lemma take_bit_length_eq [simp]:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
   991
  \<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
   992
  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
   993
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   994
lemma bit_word_of_int_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   995
  \<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
   996
  by transfer rule
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   997
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   998
lemma bit_uint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   999
  \<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
  1000
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1001
  by transfer (simp add: bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1002
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1003
lemma bit_sint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1004
  \<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
  1005
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1006
  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
  1007
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1008
lemma bit_word_ucast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1009
  \<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
  1010
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1011
  by transfer (simp add: bit_take_bit_iff ac_simps)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1012
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1013
lemma bit_word_scast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1014
  \<open>bit (scast w :: 'b::len word) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1015
    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
  1016
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1017
  by transfer (auto simp add: bit_signed_take_bit_iff le_less min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1018
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1019
lift_definition shiftl1 :: \<open>'a::len word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1020
  is \<open>(*) 2\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1021
  by (auto simp add: take_bit_eq_mod intro: mod_mult_cong)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1022
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1023
lemma shiftl1_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1024
  \<open>shiftl1 w = word_of_int (2 * uint w)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1025
  by transfer (simp add: take_bit_eq_mod mod_simps)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1026
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1027
lemma shiftl1_eq_mult_2:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1028
  \<open>shiftl1 = (*) 2\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1029
  by (rule ext, transfer) simp
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1030
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1031
lemma bit_shiftl1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1032
  \<open>bit (shiftl1 w) n \<longleftrightarrow> 0 < n \<and> n < LENGTH('a) \<and> bit w (n - 1)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1033
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1034
  by (simp add: shiftl1_eq_mult_2 bit_double_iff exp_eq_zero_iff not_le) (simp add: ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1035
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1036
lift_definition shiftr1 :: \<open>'a::len word \<Rightarrow> 'a word\<close>
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1037
  \<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1038
  is \<open>\<lambda>k. take_bit LENGTH('a) k div 2\<close> by simp
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
  1039
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1040
lemma shiftr1_eq_div_2:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1041
  \<open>shiftr1 w = w div 2\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1042
  by transfer simp
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1043
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1044
lemma bit_shiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1045
  \<open>bit (shiftr1 w) n \<longleftrightarrow> bit w (Suc n)\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1046
  by transfer (auto simp flip: bit_Suc simp add: bit_take_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1047
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1048
lemma shiftr1_eq:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1049
  \<open>shiftr1 w = word_of_int (uint w div 2)\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1050
  by transfer simp
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1051
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1052
instantiation word :: (len) ring_bit_operations
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1053
begin
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1054
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1055
lift_definition not_word :: \<open>'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1056
  is not
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1057
  by (simp add: take_bit_not_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1058
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1059
lift_definition and_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1060
  is \<open>and\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1061
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1062
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1063
lift_definition or_word :: \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1064
  is or
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1065
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1066
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1067
lift_definition xor_word ::  \<open>'a word \<Rightarrow> 'a word \<Rightarrow> 'a word\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1068
  is xor
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1069
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1070
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1071
lift_definition mask_word :: \<open>nat \<Rightarrow> 'a word\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1072
  is mask
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1073
  .
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1074
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1075
instance by (standard; transfer)
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1076
  (auto simp add: minus_eq_not_minus_1 mask_eq_exp_minus_1
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1077
    bit_not_iff bit_and_iff bit_or_iff bit_xor_iff)
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1078
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1079
end
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1080
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1081
context
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1082
  includes lifting_syntax
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1083
begin
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1084
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1085
lemma set_bit_word_transfer [transfer_rule]:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1086
  \<open>((=) ===> pcr_word ===> pcr_word) set_bit set_bit\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1087
  by (unfold set_bit_def) transfer_prover
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1088
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1089
lemma unset_bit_word_transfer [transfer_rule]:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1090
  \<open>((=) ===> pcr_word ===> pcr_word) unset_bit unset_bit\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1091
  by (unfold unset_bit_def) transfer_prover
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1092
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1093
lemma flip_bit_word_transfer [transfer_rule]:
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1094
  \<open>((=) ===> pcr_word ===> pcr_word) flip_bit flip_bit\<close>
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1095
  by (unfold flip_bit_def) transfer_prover
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1096
72242
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1097
lemma signed_take_bit_word_transfer [transfer_rule]:
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1098
  \<open>((=) ===> pcr_word ===> pcr_word)
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1099
    (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1100
    (signed_take_bit :: nat \<Rightarrow> 'a word \<Rightarrow> 'a word)\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1101
proof -
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1102
  let ?K = \<open>\<lambda>n (k :: int). take_bit (min LENGTH('a) n) k OR of_bool (n < LENGTH('a) \<and> bit k n) * NOT (mask n)\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1103
  let ?W = \<open>\<lambda>n (w :: 'a word). take_bit n w OR of_bool (bit w n) * NOT (mask n)\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1104
  have \<open>((=) ===> pcr_word ===> pcr_word) ?K ?W\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1105
    by transfer_prover
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1106
  also have \<open>?K = (\<lambda>n k. signed_take_bit n (take_bit LENGTH('a::len) k))\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1107
    by (simp add: fun_eq_iff signed_take_bit_def bit_take_bit_iff ac_simps)
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1108
  also have \<open>?W = signed_take_bit\<close>
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1109
    by (simp add: fun_eq_iff signed_take_bit_def)
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1110
  finally show ?thesis .
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1111
qed
bb002df3e82e more on conversions
haftmann
parents: 72241
diff changeset
  1112
72009
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1113
end
febdd4eead56 more on single-bit operations
haftmann
parents: 72000
diff changeset
  1114
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1115
instantiation word :: (len) semiring_bit_syntax
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1116
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1117
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1118
lift_definition test_bit_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> bool\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1119
  is \<open>\<lambda>k n. n < LENGTH('a) \<and> bit k n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1120
proof
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1121
  fix k l :: int and n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1122
  assume *: \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1123
  show \<open>n < LENGTH('a) \<and> bit k n \<longleftrightarrow> n < LENGTH('a) \<and> bit l n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1124
  proof (cases \<open>n < LENGTH('a)\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1125
    case True
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1126
    from * have \<open>bit (take_bit LENGTH('a) k) n \<longleftrightarrow> bit (take_bit LENGTH('a) l) n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1127
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1128
    then show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1129
      by (simp add: bit_take_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1130
  next
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1131
    case False
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1132
    then show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1133
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1134
  qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1135
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1136
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1137
lemma test_bit_word_eq:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1138
  \<open>test_bit = (bit :: 'a word \<Rightarrow> _)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1139
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1140
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1141
lemma bit_word_iff_drop_bit_and [code]:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1142
  \<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
  1143
  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
  1144
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1145
lemma [code]:
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1146
  \<open>test_bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1147
  by (simp add: test_bit_word_eq bit_word_iff_drop_bit_and)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1148
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1149
lift_definition shiftl_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1150
  is \<open>\<lambda>k n. push_bit n k\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1151
proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1152
  show \<open>take_bit LENGTH('a) (push_bit n k) = take_bit LENGTH('a) (push_bit n l)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1153
    if \<open>take_bit LENGTH('a) k = take_bit LENGTH('a) l\<close> for k l :: int and n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1154
  proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1155
    from that
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1156
    have \<open>take_bit (LENGTH('a) - n) (take_bit LENGTH('a) k)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1157
      = take_bit (LENGTH('a) - n) (take_bit LENGTH('a) l)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1158
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1159
    moreover have \<open>min (LENGTH('a) - n) LENGTH('a) = LENGTH('a) - n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1160
      by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1161
    ultimately show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1162
      by (simp add: take_bit_push_bit)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1163
  qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1164
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1165
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1166
lemma shiftl_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1167
  \<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1168
  by transfer rule
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1169
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1170
lift_definition shiftr_word :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1171
  is \<open>\<lambda>k n. drop_bit n (take_bit LENGTH('a) k)\<close> by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1172
  
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1173
lemma shiftr_word_eq:
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1174
  \<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1175
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1176
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1177
instance
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1178
  by (standard; transfer) simp_all
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1179
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1180
end
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  1181
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1182
lemma shiftl_code [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1183
  \<open>w << n = w * 2 ^ n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1184
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1185
  by transfer (simp add: push_bit_eq_mult)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1186
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1187
lemma shiftl1_code [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1188
  \<open>shiftl1 w = w << 1\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1189
  by transfer (simp add: push_bit_eq_mult ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1190
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1191
lemma uint_shiftr_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1192
  \<open>uint (w >> n) = uint w div 2 ^ n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1193
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1194
  by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit min_def le_less less_diff_conv)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1195
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1196
lemma shiftr1_code [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1197
  \<open>shiftr1 w = w >> 1\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1198
  by transfer (simp add: drop_bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1199
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1200
lemma word_test_bit_def: 
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1201
  \<open>test_bit a = bit (uint a)\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1202
  by transfer (simp add: fun_eq_iff bit_take_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1203
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1204
lemma shiftl_def:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1205
  \<open>w << n = (shiftl1 ^^ n) w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1206
proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1207
  have \<open>push_bit n = (((*) 2 ^^ n) :: int \<Rightarrow> int)\<close> for n
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1208
    by (induction n) (simp_all add: fun_eq_iff funpow_swap1, simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1209
  then show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1210
    by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1211
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1212
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1213
lemma shiftr_def:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1214
  \<open>w >> n = (shiftr1 ^^ n) w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1215
proof -
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1216
  have \<open>drop_bit n = (((\<lambda>k::int. k div 2) ^^ n))\<close> for n
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1217
    by (rule sym, induction n)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1218
       (simp_all add: fun_eq_iff drop_bit_Suc flip: drop_bit_half)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1219
  then show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1220
    apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1221
    apply simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1222
    apply (metis bintrunc_bintrunc rco_bintr)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1223
    done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1224
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1225
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1226
lemma bit_shiftl_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1227
  \<open>bit (w << m) n \<longleftrightarrow> m \<le> n \<and> n < LENGTH('a) \<and> bit w (n - m)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1228
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1229
  by (simp add: shiftl_word_eq bit_push_bit_iff exp_eq_zero_iff not_le)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1230
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1231
lemma [code]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1232
  \<open>push_bit n w = w << n\<close> for w :: \<open>'a::len word\<close>
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1233
  by (simp add: shiftl_word_eq)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1234
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1235
lemma bit_shiftr_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1236
  \<open>bit (w >> m) n \<longleftrightarrow> bit w (m + n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1237
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1238
  by (simp add: shiftr_word_eq bit_drop_bit_eq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1239
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1240
lemma [code]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1241
  \<open>drop_bit n w = w >> n\<close> for w :: \<open>'a::len word\<close>
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1242
  by (simp add: shiftr_word_eq)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1243
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1244
lemma [code]:
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1245
  \<open>uint (take_bit n w) = (if n < LENGTH('a::len) then take_bit n (uint w) else uint w)\<close>
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1246
  for w :: \<open>'a::len word\<close>
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1247
  by transfer (simp add: min_def)
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1248
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1249
lemma [code]:
72083
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1250
  \<open>uint (mask n :: 'a::len word) = mask (min LENGTH('a) n)\<close>
3ec876181527 further refinement of code equations for mask operation
haftmann
parents: 72082
diff changeset
  1251
  by transfer simp
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1252
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1253
lemma [code_abbrev]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1254
  \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close>
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1255
  by (fact push_bit_of_1)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1256
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1257
lemma
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1258
  word_not_def [code]: "NOT (a::'a::len word) = word_of_int (NOT (uint a))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1259
    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
  1260
    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
  1261
    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
  1262
  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
  1263
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1264
lemma [code abstract]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1265
  \<open>uint (v AND w) = uint v AND uint w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1266
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1267
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1268
lemma [code abstract]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1269
  \<open>uint (v OR w) = uint v OR uint w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1270
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1271
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1272
lemma [code abstract]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1273
  \<open>uint (v XOR w) = uint v XOR uint w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1274
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1275
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1276
lift_definition setBit :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1277
  is \<open>\<lambda>k n. set_bit n k\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1278
  by (simp add: take_bit_set_bit_eq)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1279
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1280
lemma set_Bit_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1281
  \<open>setBit w n = set_bit n w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1282
  by transfer simp
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1283
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1284
lemma bit_setBit_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1285
  \<open>bit (setBit w m) n \<longleftrightarrow> (m = n \<and> n < LENGTH('a) \<or> bit w n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1286
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1287
  by transfer (auto simp add: bit_set_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1288
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1289
lift_definition clearBit :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1290
  is \<open>\<lambda>k n. unset_bit n k\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1291
  by (simp add: take_bit_unset_bit_eq)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1292
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1293
lemma clear_Bit_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1294
  \<open>clearBit w n = unset_bit n w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1295
  by transfer simp
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1296
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1297
lemma bit_clearBit_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1298
  \<open>bit (clearBit w m) n \<longleftrightarrow> m \<noteq> n \<and> bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1299
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1300
  by transfer (auto simp add: bit_unset_bit_iff)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1301
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1302
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
  1303
  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
  1304
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1305
lemma even_word_iff [code]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1306
  \<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
  1307
  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
  1308
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1309
lemma map_bit_range_eq_if_take_bit_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1310
  \<open>map (bit k) [0..<n] = map (bit l) [0..<n]\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1311
  if \<open>take_bit n k = take_bit n l\<close> for k l :: int
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1312
using that proof (induction n arbitrary: k l)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1313
  case 0
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1314
  then show ?case
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1315
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1316
next
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1317
  case (Suc n)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1318
  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
  1319
    by (simp add: take_bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1320
  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
  1321
    by (rule Suc.IH)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1322
  moreover have \<open>bit (r div 2) = bit r \<circ> Suc\<close> for r :: int
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1323
    by (simp add: fun_eq_iff bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1324
  moreover from Suc.prems have \<open>even k \<longleftrightarrow> even l\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1325
    by (auto simp add: take_bit_Suc elim!: evenE oddE) arith+
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1326
  ultimately show ?case
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1327
    by (simp only: map_Suc_upt upt_conv_Cons flip: list.map_comp) simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1328
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1329
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1330
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1331
subsection \<open>Type-definition locale instantiations\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1332
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1333
definition uints :: "nat \<Rightarrow> int set"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1334
  \<comment> \<open>the sets of integers representing the words\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1335
  where "uints n = range (take_bit n)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1336
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1337
definition sints :: "nat \<Rightarrow> int set"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1338
  where "sints n = range (signed_take_bit (n - 1))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1339
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1340
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1341
  by (simp add: uints_def range_bintrunc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1342
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1343
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1344
  by (simp add: sints_def range_sbintrunc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1345
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1346
definition unats :: "nat \<Rightarrow> nat set"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1347
  where "unats n = {i. i < 2 ^ n}"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1348
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1349
\<comment> \<open>naturals\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1350
lemma uints_unats: "uints n = int ` unats n"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1351
  apply (unfold unats_def uints_num)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1352
  apply safe
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1353
    apply (rule_tac image_eqI)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1354
     apply (erule_tac nat_0_le [symmetric])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1355
  by auto
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1356
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1357
lemma unats_uints: "unats n = nat ` uints n"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1358
  by (auto simp: uints_unats image_iff)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1359
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1360
lemma td_ext_uint:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1361
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1362
    (\<lambda>w::int. w mod 2 ^ LENGTH('a))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1363
  apply (unfold td_ext_def')
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1364
  apply transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1365
  apply (simp add: uints_num take_bit_eq_mod)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1366
  done
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1367
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1368
interpretation word_uint:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1369
  td_ext
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1370
    "uint::'a::len word \<Rightarrow> int"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1371
    word_of_int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1372
    "uints (LENGTH('a::len))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1373
    "\<lambda>w. w mod 2 ^ LENGTH('a::len)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1374
  by (fact td_ext_uint)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1375
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1376
lemmas td_uint = word_uint.td_thm
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1377
lemmas int_word_uint = word_uint.eq_norm
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1378
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1379
lemma td_ext_ubin:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1380
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1381
    (take_bit (LENGTH('a)))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1382
  apply standard
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1383
  apply transfer
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1384
  apply simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1385
  done
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1386
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1387
interpretation word_ubin:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1388
  td_ext
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1389
    "uint::'a::len word \<Rightarrow> int"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1390
    word_of_int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1391
    "uints (LENGTH('a::len))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1392
    "take_bit (LENGTH('a::len))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1393
  by (fact td_ext_ubin)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1394
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1395
lemma td_ext_unat [OF refl]:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1396
  "n = LENGTH('a::len) \<Longrightarrow>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1397
    td_ext (unat :: 'a word \<Rightarrow> nat) of_nat (unats n) (\<lambda>i. i mod 2 ^ n)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1398
  apply (standard; transfer)
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
  1399
     apply (simp_all add: unats_def take_bit_int_less_exp take_bit_of_nat take_bit_nat_eq_self_iff
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
  1400
      flip: take_bit_eq_mod)
72244
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1401
  done
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1402
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1403
lemmas unat_of_nat = td_ext_unat [THEN td_ext.eq_norm]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1404
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1405
interpretation word_unat:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1406
  td_ext
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1407
    "unat::'a::len word \<Rightarrow> nat"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1408
    of_nat
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1409
    "unats (LENGTH('a::len))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1410
    "\<lambda>i. i mod 2 ^ LENGTH('a::len)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1411
  by (rule td_ext_unat)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1412
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1413
lemmas td_unat = word_unat.td_thm
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1414
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1415
lemmas unat_lt2p [iff] = word_unat.Rep [unfolded unats_def mem_Collect_eq]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1416
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1417
lemma unat_le: "y \<le> unat z \<Longrightarrow> y \<in> unats (LENGTH('a))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1418
  for z :: "'a::len word"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1419
  apply (unfold unats_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1420
  apply clarsimp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1421
  apply (rule xtrans, rule unat_lt2p, assumption)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1422
  done
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1423
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1424
lemma td_ext_sbin:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1425
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1426
    (signed_take_bit (LENGTH('a) - 1))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1427
  apply (unfold td_ext_def' sint_uint)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1428
  apply (simp add : word_ubin.eq_norm)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1429
  apply (cases "LENGTH('a)")
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1430
   apply (auto simp add : sints_def)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1431
  apply (rule sym [THEN trans])
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1432
   apply (rule word_ubin.Abs_norm)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1433
  apply (simp only: bintrunc_sbintrunc)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1434
  apply (drule sym)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1435
  apply simp
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1436
  done
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1437
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1438
lemma td_ext_sint:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1439
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1440
     (\<lambda>w. (w + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1441
         2 ^ (LENGTH('a) - 1))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1442
  using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1443
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1444
text \<open>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1445
  We do \<open>sint\<close> before \<open>sbin\<close>, before \<open>sint\<close> is the user version
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1446
  and interpretations do not produce thm duplicates. I.e.
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1447
  we get the name \<open>word_sint.Rep_eqD\<close>, but not \<open>word_sbin.Req_eqD\<close>,
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1448
  because the latter is the same thm as the former.
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1449
\<close>
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1450
interpretation word_sint:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1451
  td_ext
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1452
    "sint ::'a::len word \<Rightarrow> int"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1453
    word_of_int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1454
    "sints (LENGTH('a::len))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1455
    "\<lambda>w. (w + 2^(LENGTH('a::len) - 1)) mod 2^LENGTH('a::len) -
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1456
      2 ^ (LENGTH('a::len) - 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1457
  by (rule td_ext_sint)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1458
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1459
interpretation word_sbin:
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1460
  td_ext
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1461
    "sint ::'a::len word \<Rightarrow> int"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1462
    word_of_int
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1463
    "sints (LENGTH('a::len))"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1464
    "signed_take_bit (LENGTH('a::len) - 1)"
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1465
  by (rule td_ext_sbin)
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1466
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1467
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1468
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1469
lemmas td_sint = word_sint.td
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1470
4b011fa5e83b restructured
haftmann
parents: 72243
diff changeset
  1471
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1472
subsection \<open>More shift operations\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1473
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1474
lift_definition sshiftr1 :: \<open>'a::len word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1475
  is \<open>\<lambda>k. take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - 1) k div 2)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1476
  by (simp flip: signed_take_bit_decr_length_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1477
 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1478
lift_definition sshiftr :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>  (infixl \<open>>>>\<close> 55)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1479
  is \<open>\<lambda>k n. take_bit LENGTH('a) (drop_bit n (signed_take_bit (LENGTH('a) - 1) k))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1480
  by (simp flip: signed_take_bit_decr_length_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1481
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1482
lift_definition bshiftr1 :: \<open>bool \<Rightarrow> 'a::len word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1483
  is \<open>\<lambda>b k. take_bit LENGTH('a) k div 2 + of_bool b * 2 ^ (LENGTH('a) - Suc 0)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1484
  by (fact arg_cong)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1485
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1486
lemma sshiftr1_eq:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1487
  \<open>sshiftr1 w = word_of_int (sint w div 2)\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1488
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1489
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1490
lemma sshiftr_eq:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1491
  \<open>w >>> n = (sshiftr1 ^^ n) w\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1492
proof -
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  1493
  have *: \<open>(\<lambda>k::int. take_bit LENGTH('a) (signed_take_bit (LENGTH('a) - Suc 0) k div 2)) ^^ Suc n =
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1494
    take_bit LENGTH('a) \<circ> drop_bit (Suc n) \<circ> signed_take_bit (LENGTH('a) - Suc 0)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1495
    for n
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1496
    apply (induction n)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1497
     apply (auto simp add: fun_eq_iff drop_bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1498
    apply (metis (no_types, lifting) Suc_pred funpow_swap1 len_gt_0 sbintrunc_bintrunc sbintrunc_rest)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1499
    done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1500
  show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1501
    apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1502
    apply simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1503
    subgoal for k n
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1504
      apply (cases n)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1505
       apply (simp_all only: *)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1506
       apply simp_all
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1507
      done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1508
    done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1509
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1510
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1511
lemma mask_eq:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1512
  \<open>mask n = (1 << n) - (1 :: 'a::len word)\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1513
  by transfer (simp add: mask_eq_exp_minus_1 push_bit_of_1) 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1514
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1515
lemma uint_sshiftr_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1516
  \<open>uint (w >>> n) = take_bit LENGTH('a) (sint w div 2 ^  n)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1517
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1518
  by transfer (simp flip: drop_bit_eq_div)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1519
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1520
lemma sshift1_code [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1521
  \<open>sshiftr1 w = w >>> 1\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1522
  by transfer (simp add: drop_bit_Suc)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1523
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1524
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1525
subsection \<open>Rotation\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1526
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1527
lift_definition word_rotr :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1528
  is \<open>\<lambda>n k. concat_bit (LENGTH('a) - n mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1529
    (drop_bit (n mod LENGTH('a)) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1530
    (take_bit (n mod LENGTH('a)) k)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1531
  subgoal for n k l
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1532
    apply (simp add: concat_bit_def nat_le_iff less_imp_le
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1533
      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
  1534
    done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1535
  done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1536
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1537
lift_definition word_rotl :: \<open>nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1538
  is \<open>\<lambda>n k. concat_bit (n mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1539
    (drop_bit (LENGTH('a) - n mod LENGTH('a)) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1540
    (take_bit (LENGTH('a) - n mod LENGTH('a)) k)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1541
  subgoal for n k l
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1542
    apply (simp add: concat_bit_def nat_le_iff less_imp_le
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1543
      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
  1544
    done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1545
  done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1546
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1547
lift_definition word_roti :: \<open>int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1548
  is \<open>\<lambda>r k. concat_bit (LENGTH('a) - nat (r mod int LENGTH('a)))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1549
    (drop_bit (nat (r mod int LENGTH('a))) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1550
    (take_bit (nat (r mod int LENGTH('a))) k)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1551
  subgoal for r k l
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1552
    apply (simp add: concat_bit_def nat_le_iff less_imp_le
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1553
      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
  1554
    done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1555
  done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1556
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1557
lemma word_rotl_eq_word_rotr [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1558
  \<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
  1559
  by (rule ext, cases \<open>n mod LENGTH('a) = 0\<close>; transfer) simp_all
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1560
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1561
lemma word_roti_eq_word_rotr_word_rotl [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1562
  \<open>word_roti i w =
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1563
    (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
  1564
proof (cases \<open>i \<ge> 0\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1565
  case True
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1566
  moreover define n where \<open>n = nat i\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1567
  ultimately have \<open>i = int n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1568
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1569
  moreover have \<open>word_roti (int n) = (word_rotr n :: _ \<Rightarrow> 'a word)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1570
    by (rule ext, transfer) (simp add: nat_mod_distrib)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1571
  ultimately show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1572
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1573
next
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1574
  case False
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1575
  moreover define n where \<open>n = nat (- i)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1576
  ultimately have \<open>i = - int n\<close> \<open>n > 0\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1577
    by simp_all
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1578
  moreover have \<open>word_roti (- int n) = (word_rotl n :: _ \<Rightarrow> 'a word)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1579
    by (rule ext, transfer)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1580
      (simp add: zmod_zminus1_eq_if flip: of_nat_mod of_nat_diff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1581
  ultimately show ?thesis
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1582
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1583
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1584
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1585
lemma bit_word_rotr_iff:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1586
  \<open>bit (word_rotr m w) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1587
    n < LENGTH('a) \<and> bit w ((n + m) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1588
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1589
proof transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1590
  fix k :: int and m n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1591
  define q where \<open>q = m mod LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1592
  have \<open>q < LENGTH('a)\<close> 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1593
    by (simp add: q_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1594
  then have \<open>q \<le> LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1595
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1596
  have \<open>m mod LENGTH('a) = q\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1597
    by (simp add: q_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1598
  moreover have \<open>(n + m) mod LENGTH('a) = (n + q) mod LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1599
    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
  1600
  moreover have \<open>n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1601
    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
  1602
    n < LENGTH('a) \<and> bit k ((n + q) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1603
    using \<open>q < LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1604
    by (cases \<open>q + n \<ge> LENGTH('a)\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1605
     (auto simp add: bit_concat_bit_iff bit_drop_bit_eq
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1606
        bit_take_bit_iff le_mod_geq ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1607
  ultimately show \<open>n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1608
    bit (concat_bit (LENGTH('a) - m mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1609
      (drop_bit (m mod LENGTH('a)) (take_bit LENGTH('a) k))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1610
      (take_bit (m mod LENGTH('a)) k)) n
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1611
    \<longleftrightarrow> n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1612
      (n + m) mod LENGTH('a) < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1613
      bit k ((n + m) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1614
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1615
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1616
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1617
lemma bit_word_rotl_iff:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1618
  \<open>bit (word_rotl m w) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1619
    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
  1620
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1621
  by (simp add: word_rotl_eq_word_rotr bit_word_rotr_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1622
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1623
lemma bit_word_roti_iff:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1624
  \<open>bit (word_roti k w) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1625
    n < LENGTH('a) \<and> bit w (nat ((int n + k) mod int LENGTH('a)))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1626
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1627
proof transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1628
  fix k l :: int and n :: nat
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1629
  define m where \<open>m = nat (k mod int LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1630
  have \<open>m < LENGTH('a)\<close> 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1631
    by (simp add: nat_less_iff m_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1632
  then have \<open>m \<le> LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1633
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1634
  have \<open>k mod int LENGTH('a) = int m\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1635
    by (simp add: nat_less_iff m_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1636
  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
  1637
    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
  1638
  moreover have \<open>n < LENGTH('a) \<and>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1639
    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
  1640
    n < LENGTH('a) \<and> bit l ((n + m) mod LENGTH('a))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1641
    using \<open>m < LENGTH('a)\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1642
    by (cases \<open>m + n \<ge> LENGTH('a)\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1643
     (auto simp add: bit_concat_bit_iff bit_drop_bit_eq
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1644
        bit_take_bit_iff nat_less_iff not_le not_less ac_simps
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1645
        le_diff_conv le_mod_geq)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1646
  ultimately show \<open>n < LENGTH('a)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1647
    \<and> bit (concat_bit (LENGTH('a) - nat (k mod int LENGTH('a)))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1648
             (drop_bit (nat (k mod int LENGTH('a))) (take_bit LENGTH('a) l))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1649
             (take_bit (nat (k mod int LENGTH('a))) l)) n \<longleftrightarrow>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1650
       n < LENGTH('a) 
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1651
    \<and> nat ((int n + k) mod int LENGTH('a)) < LENGTH('a)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1652
    \<and> bit l (nat ((int n + k) mod int LENGTH('a)))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1653
    by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1654
qed
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1655
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1656
lemma uint_word_rotr_eq [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1657
  \<open>uint (word_rotr n w) = concat_bit (LENGTH('a) - n mod LENGTH('a))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1658
    (drop_bit (n mod LENGTH('a)) (uint w))
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1659
    (uint (take_bit (n mod LENGTH('a)) w))\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1660
  for w :: \<open>'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1661
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1662
  apply (simp add: concat_bit_def take_bit_drop_bit push_bit_take_bit min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1663
  using mod_less_divisor not_less apply blast
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1664
  done
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1665
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1666
    
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1667
subsection \<open>Split and cat operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1668
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1669
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
  1670
  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
  1671
  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
  1672
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1673
lemma word_cat_eq:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1674
  \<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
  1675
  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
  1676
  by transfer (simp add: concat_bit_eq ac_simps)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1677
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1678
lemma word_cat_eq' [code]:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1679
  \<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
  1680
  for a :: \<open>'a::len word\<close> and b :: \<open>'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1681
  by transfer simp
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1682
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1683
lemma bit_word_cat_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1684
  \<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
  1685
  for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1686
  by transfer (simp add: bit_concat_bit_iff bit_take_bit_iff)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1687
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
  1688
definition word_split :: "'a::len word \<Rightarrow> 'b::len word \<times> 'c::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1689
  where "word_split a =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1690
    (case bin_split (LENGTH('c)) (uint a) of
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1691
      (u, v) \<Rightarrow> (word_of_int u, word_of_int v))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1692
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1693
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
  1694
  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
  1695
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1696
lemma word_rcat_eq:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1697
  \<open>word_rcat ws = word_of_int (bin_rcat (LENGTH('a::len)) (map uint ws))\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1698
  for ws :: \<open>'a::len word list\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1699
  apply (simp add: word_rcat_def bin_rcat_def rev_map)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1700
  apply transfer
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1701
  apply (simp add: horner_sum_foldr foldr_map comp_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  1702
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1703
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
  1704
definition word_rsplit :: "'a::len word \<Rightarrow> 'b::len word list"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1705
  where "word_rsplit w = map word_of_int (bin_rsplit (LENGTH('b)) (LENGTH('a), uint w))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1706
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
  1707
abbreviation (input) max_word :: \<open>'a::len word\<close>
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  1708
  \<comment> \<open>Largest representable machine integer.\<close>
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  1709
  where "max_word \<equiv> - 1"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1710
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1711
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1712
subsection \<open>Theorems about typedefs\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  1713
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1714
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1715
  by (auto simp: sint_uint word_ubin.eq_norm sbintrunc_bintrunc_lt)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1716
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1717
lemma uint_sint: "uint w = take_bit (LENGTH('a)) (sint w)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1718
  for w :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1719
  by (auto simp: sint_uint bintrunc_sbintrunc_le)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1720
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1721
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
  1722
  for w :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1723
  apply (subst word_ubin.norm_Rep [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1724
  apply (simp only: bintrunc_bintrunc_min word_size)
54863
82acc20ded73 prefer more canonical names for lemmas on min/max
haftmann
parents: 54854
diff changeset
  1725
  apply (simp add: min.absorb2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1726
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1727
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1728
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
  1729
  "LENGTH('a::len) \<le> n \<Longrightarrow>
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1730
    word_of_int (take_bit n w) = (word_of_int w :: 'a word)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1731
  by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1732
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1733
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1734
  by (fact uints_def [unfolded no_bintr_alt1])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1735
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1736
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
  1737
  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
  1738
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1739
declare word_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1740
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1741
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
  1742
  by (simp only: word_numeral_alt wi_hom_neg)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1743
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1744
declare word_neg_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1745
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1746
lemma uint_bintrunc [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1747
  "uint (numeral bin :: 'a word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1748
    take_bit (LENGTH('a::len)) (numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1749
  unfolding word_numeral_alt by (rule word_ubin.eq_norm)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1750
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1751
lemma uint_bintrunc_neg [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1752
  "uint (- numeral bin :: 'a word) = take_bit (LENGTH('a::len)) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1753
  by (simp only: word_neg_numeral_alt word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1754
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1755
lemma sint_sbintrunc [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1756
  "sint (numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1757
  by (simp only: word_numeral_alt word_sbin.eq_norm)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1758
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1759
lemma sint_sbintrunc_neg [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1760
  "sint (- numeral bin :: 'a word) = signed_take_bit (LENGTH('a::len) - 1) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1761
  by (simp only: word_neg_numeral_alt word_sbin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1762
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1763
lemma unat_bintrunc [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1764
  "unat (numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (numeral bin))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1765
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1766
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1767
lemma unat_bintrunc_neg [simp]:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1768
  "unat (- numeral bin :: 'a::len word) = nat (take_bit (LENGTH('a)) (- numeral bin))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1769
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1770
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1771
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
  1772
  for v w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1773
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1774
  apply (rule word_uint.Rep_eqD)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1775
  apply (rule box_equals)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1776
    defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1777
    apply (rule word_ubin.norm_Rep)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1778
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1779
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1780
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1781
lemma uint_ge_0 [iff]: "0 \<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
  1782
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1783
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1784
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1785
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
  1786
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1787
  using word_uint.Rep [of x] by (simp add: uints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1788
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1789
lemma sint_ge: "- (2 ^ (LENGTH('a) - 1)) \<le> sint x"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1790
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1791
  using word_sint.Rep [of x] by (simp add: sints_num)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1792
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1793
lemma sint_lt: "sint x < 2 ^ (LENGTH('a) - 1)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1794
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1795
  using word_sint.Rep [of x] by (simp add: sints_num)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1796
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1797
lemma sign_uint_Pls [simp]: "bin_sign (uint x) = 0"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1798
  by (simp add: sign_Pls_ge_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1799
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1800
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
  1801
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1802
  by (simp only: diff_less_0_iff_less uint_lt2p)
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1803
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1804
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
  1805
  for x :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1806
  by (simp only: not_le uint_m2p_neg)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1807
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1808
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
  1809
  for w :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  1810
  by (metis bintr_lt2p bintr_uint)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1811
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1812
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
  1813
  by (fact uint_ge_0 [THEN leD, THEN antisym_conv1])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1814
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  1815
lemma uint_nat: "uint w = int (unat w)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1816
  by transfer simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1817
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
  1818
lemma uint_numeral: "uint (numeral b :: 'a::len word) = numeral b mod 2 ^ LENGTH('a)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1819
  by (simp only: word_numeral_alt int_word_uint)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1820
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
  1821
lemma uint_neg_numeral: "uint (- numeral b :: 'a::len word) = - numeral b mod 2 ^ LENGTH('a)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1822
  by (simp only: word_neg_numeral_alt int_word_uint)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1823
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
  1824
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
  1825
  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
  1826
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1827
lemma sint_numeral:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1828
  "sint (numeral b :: 'a::len word) =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1829
    (numeral b +
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1830
      2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) -
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1831
      2 ^ (LENGTH('a) - 1)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1832
  unfolding word_numeral_alt by (rule int_word_sint)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1833
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1834
lemma word_of_int_0 [simp, code_post]: "word_of_int 0 = 0"
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1835
  unfolding word_0_wi ..
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1836
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1837
lemma word_of_int_1 [simp, code_post]: "word_of_int 1 = 1"
45958
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1838
  unfolding word_1_wi ..
c28235388c43 simplify some proofs
huffman
parents: 45957
diff changeset
  1839
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1840
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
  1841
  by (simp add: wi_hom_syms)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  1842
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
  1843
lemma word_of_int_numeral [simp] : "(word_of_int (numeral bin) :: 'a::len word) = numeral bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1844
  by (simp only: word_numeral_alt)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1845
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1846
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
  1847
  "(word_of_int (- numeral bin) :: 'a::len word) = - numeral bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1848
  by (simp only: word_numeral_alt wi_hom_syms)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1849
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1850
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
  1851
  "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
  1852
  by transfer (simp add: take_bit_eq_mod)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1853
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1854
lemma word_int_split:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1855
  "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
  1856
    (\<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
  1857
  by transfer (auto simp add: take_bit_eq_mod)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1858
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1859
lemma word_int_split_asm:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1860
  "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
  1861
    (\<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
  1862
  by transfer (auto simp add: take_bit_eq_mod)
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1863
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1864
lemmas uint_range' = word_uint.Rep [unfolded uints_num mem_Collect_eq]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1865
lemmas sint_range' = word_sint.Rep [unfolded One_nat_def sints_num mem_Collect_eq]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1866
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1867
lemma uint_range_size: "0 \<le> uint w \<and> uint w < 2 ^ size w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1868
  unfolding word_size by (rule uint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1869
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1870
lemma sint_range_size: "- (2 ^ (size w - Suc 0)) \<le> sint w \<and> sint w < 2 ^ (size w - Suc 0)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1871
  unfolding word_size by (rule sint_range')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1872
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1873
lemma sint_above_size: "2 ^ (size w - 1) \<le> x \<Longrightarrow> sint w < x"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1874
  for w :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1875
  unfolding word_size by (rule less_le_trans [OF sint_lt])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1876
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1877
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
  1878
  for w :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1879
  unfolding word_size by (rule order_trans [OF _ sint_ge])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1880
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1881
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1882
subsection \<open>Testing bits\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  1883
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1884
lemma test_bit_eq_iff: "test_bit u = test_bit v \<longleftrightarrow> u = 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
  1885
  for u v :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1886
  unfolding word_test_bit_def by (simp add: bin_nth_eq_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1887
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1888
lemma test_bit_size [rule_format] : "w !! n \<longrightarrow> n < size 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
  1889
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1890
  apply (unfold word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1891
  apply (subst word_ubin.norm_Rep [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1892
  apply (simp only: nth_bintr word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1893
  apply fast
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1894
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1895
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1896
lemma word_eq_iff: "x = y \<longleftrightarrow> (\<forall>n<LENGTH('a). x !! n = y !! n)" (is \<open>?P \<longleftrightarrow> ?Q\<close>)
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
  1897
  for x y :: "'a::len word"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1898
proof
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1899
  assume ?P
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1900
  then show ?Q
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1901
    by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1902
next
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1903
  assume ?Q
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1904
  then have *: \<open>bit (uint x) n \<longleftrightarrow> bit (uint y) n\<close> if \<open>n < LENGTH('a)\<close> for n
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  1905
    using that by (simp add: word_test_bit_def)
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1906
  show ?P
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1907
  proof (rule word_uint_eqI, rule bit_eqI, rule iffI)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1908
    fix n
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1909
    assume \<open>bit (uint x) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1910
    then have \<open>n < LENGTH('a)\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1911
      by (simp add: bit_take_bit_iff uint.rep_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1912
    with * \<open>bit (uint x) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1913
    show \<open>bit (uint y) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1914
      by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1915
  next
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1916
    fix n
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1917
    assume \<open>bit (uint y) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1918
    then have \<open>n < LENGTH('a)\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1919
      by (simp add: bit_take_bit_iff uint.rep_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1920
    with * \<open>bit (uint y) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1921
    show \<open>bit (uint x) n\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1922
      by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1923
  qed
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  1924
qed  
46021
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
  1925
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1926
lemma word_eqI: "(\<And>n. n < size u \<longrightarrow> u !! n = v !! n) \<Longrightarrow> u = 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
  1927
  for u :: "'a::len word"
46021
272c63f83398 add lemma word_eq_iff
huffman
parents: 46020
diff changeset
  1928
  by (simp add: word_size word_eq_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1929
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1930
lemma word_eqD: "u = v \<Longrightarrow> u !! x = v !! 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
  1931
  for u v :: "'a::len word"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1932
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1933
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1934
lemma test_bit_bin': "w !! n \<longleftrightarrow> n < size w \<and> bin_nth (uint w) n"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1935
  by (simp add: word_test_bit_def word_size nth_bintr [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1936
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1937
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1938
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1939
lemma bin_nth_uint_imp: "bin_nth (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
  1940
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1941
  apply (rule nth_bintr [THEN iffD1, THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1942
  apply (subst word_ubin.norm_Rep)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1943
  apply assumption
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1944
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1945
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1946
lemma bin_nth_sint:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1947
  "LENGTH('a) \<le> n \<Longrightarrow>
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1948
    bin_nth (sint w) n = bin_nth (sint w) (LENGTH('a) - 1)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1949
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1950
  apply (subst word_sbin.norm_Rep [symmetric])
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1951
  apply (auto simp add: nth_sbintr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1952
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1953
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1954
lemmas bintr_num =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1955
  word_ubin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1956
lemmas sbintr_num =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1957
  word_sbin.norm_eq_iff [of "numeral a" "numeral b", symmetric, folded word_numeral_alt] for a b
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1958
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1959
lemma num_of_bintr':
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1960
  "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
  1961
    numeral a = (numeral b :: 'a word)"
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1962
  unfolding bintr_num by (erule subst, simp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1963
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1964
lemma num_of_sbintr':
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  1965
  "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
  1966
    numeral a = (numeral b :: 'a word)"
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1967
  unfolding sbintr_num by (erule subst, simp)
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1968
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1969
lemma num_abs_bintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1970
  "(numeral x :: 'a word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1971
    word_of_int (take_bit (LENGTH('a::len)) (numeral x))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1972
  by (simp only: word_ubin.Abs_norm word_numeral_alt)
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1973
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1974
lemma num_abs_sbintr:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1975
  "(numeral x :: 'a word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  1976
    word_of_int (signed_take_bit (LENGTH('a::len) - 1) (numeral x))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1977
  by (simp only: word_sbin.Abs_norm word_numeral_alt)
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  1978
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1979
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1980
  \<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
  1981
  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
  1982
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1983
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1984
lemma bit_ucast_iff:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1985
  \<open>bit (ucast a :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a::len) \<and> Parity.bit a n\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1986
  by transfer (simp add: bit_take_bit_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1987
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1988
lemma ucast_id [simp]: "ucast w = w"
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1989
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1990
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1991
lemma scast_id [simp]: "scast w = w"
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1992
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1993
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
  1994
lemma nth_ucast: "(ucast w::'a::len word) !! n = (w !! n \<and> n < LENGTH('a))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  1995
  by transfer (simp add: bit_take_bit_iff ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1996
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1997
lemma ucast_mask_eq:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  1998
  \<open>ucast (mask n :: 'b word) = mask (min LENGTH('b::len) n)\<close>
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1999
  by (simp add: bit_eq_iff) (auto simp add: bit_mask_iff bit_ucast_iff exp_eq_zero_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  2000
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2001
\<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
  2002
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
  2003
  "ucast (numeral w :: 'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2004
    word_of_int (take_bit (LENGTH('a)) (numeral w))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2005
  by transfer simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2006
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2007
(* TODO: neg_numeral *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2008
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  2009
lemma scast_sbintr [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2010
  "scast (numeral w ::'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2011
    word_of_int (signed_take_bit (LENGTH('a) - Suc 0) (numeral w))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2012
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2013
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
  2014
lemma source_size: "source_size (c::'a::len word \<Rightarrow> _) = LENGTH('a)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2015
  by transfer simp
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2016
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
  2017
lemma target_size: "target_size (c::_ \<Rightarrow> 'b::len word) = LENGTH('b)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2018
  by transfer simp
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2019
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2020
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
  2021
  for c :: "'a::len word \<Rightarrow> 'b::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2022
  by transfer simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2023
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2024
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
  2025
  for c :: "'a::len word \<Rightarrow> 'b::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2026
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2027
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2028
lemma is_up_down:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2029
  \<open>is_up c \<longleftrightarrow> is_down d\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2030
  for c :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2031
  and d :: \<open>'b::len word \<Rightarrow> 'a::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2032
  by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2033
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2034
context
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2035
  fixes dummy_types :: \<open>'a::len \<times> 'b::len\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2036
begin
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2037
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2038
private abbreviation (input) UCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2039
  where \<open>UCAST == ucast\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2040
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2041
private abbreviation (input) SCAST :: \<open>'a::len word \<Rightarrow> 'b::len word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2042
  where \<open>SCAST == scast\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2043
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2044
lemma down_cast_same:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2045
  \<open>UCAST = scast\<close> if \<open>is_down UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2046
  by (rule ext, use that in transfer) (simp add: take_bit_signed_take_bit)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2047
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2048
lemma sint_up_scast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2049
  \<open>sint (SCAST w) = sint w\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2050
  using that by transfer (simp add: min_def Suc_leI le_diff_iff)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2051
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2052
lemma uint_up_ucast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2053
  \<open>uint (UCAST w) = uint w\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2054
  using that by transfer (simp add: min_def)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2055
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2056
lemma ucast_up_ucast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2057
  \<open>ucast (UCAST w) = ucast w\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2058
  using that by transfer (simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2059
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2060
lemma ucast_up_ucast_id:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2061
  \<open>ucast (UCAST w) = w\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2062
  using that by (simp add: ucast_up_ucast)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2063
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2064
lemma scast_up_scast:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2065
  \<open>scast (SCAST w) = scast w\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2066
  using that by transfer (simp add: ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2067
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2068
lemma scast_up_scast_id:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2069
  \<open>scast (SCAST w) = w\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2070
  using that by (simp add: scast_up_scast)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2071
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2072
lemma isduu:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2073
  \<open>is_up UCAST\<close> if \<open>is_down d\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2074
    for d :: \<open>'b word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2075
  using that is_up_down [of UCAST d] by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2076
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2077
lemma isdus:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2078
  \<open>is_up SCAST\<close> if \<open>is_down d\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2079
    for d :: \<open>'b word \<Rightarrow> 'a word\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2080
  using that is_up_down [of SCAST d] by simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2081
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2082
lemmas ucast_down_ucast_id = isduu [THEN ucast_up_ucast_id]
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2083
lemmas scast_down_scast_id = isdus [THEN scast_up_scast_id]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2084
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2085
lemma up_ucast_surj:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2086
  \<open>surj (ucast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2087
  by (rule surjI) (use that in \<open>rule ucast_up_ucast_id\<close>)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2088
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2089
lemma up_scast_surj:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2090
  \<open>surj (scast :: 'b word \<Rightarrow> 'a word)\<close> if \<open>is_up SCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2091
  by (rule surjI) (use that in \<open>rule scast_up_scast_id\<close>)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2092
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2093
lemma down_ucast_inj:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2094
  \<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
  2095
  by (rule inj_on_inverseI) (use that in \<open>rule ucast_down_ucast_id\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2096
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2097
lemma down_scast_inj:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2098
  \<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
  2099
  by (rule inj_on_inverseI) (use that in \<open>rule scast_down_scast_id\<close>)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2100
  
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2101
lemma ucast_down_wi:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2102
  \<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
  2103
  using that by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2104
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2105
lemma ucast_down_no:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2106
  \<open>UCAST (numeral bin) = numeral bin\<close> if \<open>is_down UCAST\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2107
  using that by transfer simp
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2108
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2109
end
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2110
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2111
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2112
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2113
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
  2114
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2115
lemma bit_last_iff:
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2116
  \<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
  2117
  for w :: \<open>'a::len word\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2118
proof -
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2119
  have \<open>?P \<longleftrightarrow> bit (uint w) (LENGTH('a) - Suc 0)\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2120
    by (simp add: bit_uint_iff)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2121
  also have \<open>\<dots> \<longleftrightarrow> ?Q\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  2122
    by (simp add: sint_uint)
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2123
  finally show ?thesis .
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2124
qed
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2125
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2126
lemma drop_bit_eq_zero_iff_not_bit_last:
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2127
  \<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
  2128
  for w :: "'a::len word"
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2129
    apply (cases \<open>LENGTH('a)\<close>)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2130
    apply simp_all
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2131
    apply (simp add: bit_iff_odd_drop_bit)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2132
    apply transfer
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2133
    apply (simp add: take_bit_drop_bit)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2134
    apply (auto simp add: drop_bit_eq_div take_bit_eq_mod min_def)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2135
    apply (auto elim!: evenE)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2136
    apply (metis div_exp_eq mod_div_trivial mult.commute nonzero_mult_div_cancel_left power_Suc0_right power_add zero_neq_numeral)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2137
    done
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  2138
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2139
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2140
subsection \<open>Word Arithmetic\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2141
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2142
lemma udvdI: "0 \<le> n \<Longrightarrow> uint b = n * uint a \<Longrightarrow> a udvd b"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2143
  by (auto simp: udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2144
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2145
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
  2146
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
  2147
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
  2148
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
  2149
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
  2150
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
  2151
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2152
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
  2153
  for v w :: "'a::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2154
  by (unfold word_size) simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2155
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2156
lemmas size_0_same = size_0_same' [unfolded word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2157
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2158
lemmas unat_eq_0 = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2159
lemmas unat_eq_zero = unat_0_iff
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2160
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2161
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2162
subsection \<open>Transferring goals from words to ints\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2163
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2164
lemma word_ths:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2165
  shows word_succ_p1: "word_succ a = a + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2166
    and word_pred_m1: "word_pred a = a - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2167
    and word_pred_succ: "word_pred (word_succ a) = a"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2168
    and word_succ_pred: "word_succ (word_pred a) = a"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2169
    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
  2170
  by (transfer, simp add: algebra_simps)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2171
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2172
lemma uint_cong: "x = y \<Longrightarrow> uint x = uint y"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2173
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2174
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2175
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
  2176
  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
  2177
  shows "uint (a + b) = (uint a + uint b) mod 2 ^ LENGTH('a::len)"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2178
    and "uint (a - b) = (uint a - uint b) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2179
    and "uint (a * b) = uint a * uint b mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2180
    and "uint (- a) = - uint a mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2181
    and "uint (word_succ a) = (uint a + 1) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2182
    and "uint (word_pred a) = (uint a - 1) mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2183
    and "uint (0 :: 'a word) = 0 mod 2 ^ LENGTH('a)"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2184
    and "uint (1 :: 'a word) = 1 mod 2 ^ LENGTH('a)"
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2185
         apply (simp_all only: word_arith_wis)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2186
         apply (simp_all add: word_uint.eq_norm)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2187
  done
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2188
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2189
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
  2190
  fixes a b :: "'a::len word"
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2191
  shows "uint (a + b) = take_bit (LENGTH('a)) (uint a + uint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2192
    and "uint (a - b) = take_bit (LENGTH('a)) (uint a - uint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2193
    and "uint (a * b) = take_bit (LENGTH('a)) (uint a * uint b)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2194
    and "uint (- a) = take_bit (LENGTH('a)) (- uint a)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2195
    and "uint (word_succ a) = take_bit (LENGTH('a)) (uint a + 1)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2196
    and "uint (word_pred a) = take_bit (LENGTH('a)) (uint a - 1)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2197
    and "uint (0 :: 'a word) = take_bit (LENGTH('a)) 0"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2198
    and "uint (1 :: 'a word) = take_bit (LENGTH('a)) 1"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2199
  by (simp_all add: uint_word_ariths take_bit_eq_mod)
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2200
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2201
lemma sint_word_ariths:
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  2202
  fixes a b :: "'a::len word"
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2203
  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
  2204
    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
  2205
    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
  2206
    and "sint (- a) = signed_take_bit (LENGTH('a) - 1) (- sint a)"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2207
    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
  2208
    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
  2209
    and "sint (0 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 0"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2210
    and "sint (1 :: 'a word) = signed_take_bit (LENGTH('a) - 1) 1"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  2211
         apply (simp_all only: word_sbin.inverse_norm [symmetric])
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  2212
         apply (simp_all add: wi_hom_syms)
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  2213
   apply transfer apply simp
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  2214
  apply transfer apply simp
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  2215
  done
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2216
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2217
lemmas uint_div_alt = word_div_def [THEN trans [OF uint_cong int_word_uint]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2218
lemmas uint_mod_alt = word_mod_def [THEN trans [OF uint_cong int_word_uint]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2219
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58061
diff changeset
  2220
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
  2221
  unfolding word_pred_m1 by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2222
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2223
lemma succ_pred_no [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2224
    "word_succ (numeral w) = numeral w + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2225
    "word_pred (numeral w) = numeral w - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2226
    "word_succ (- numeral w) = - numeral w + 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2227
    "word_pred (- numeral w) = - numeral w - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2228
  by (simp_all add: word_succ_p1 word_pred_m1)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2229
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2230
lemma word_sp_01 [simp]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2231
  "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
  2232
  by (simp_all add: word_succ_p1 word_pred_m1)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2233
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2234
\<comment> \<open>alternative approach to lifting arithmetic equalities\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2235
lemma word_of_int_Ex: "\<exists>y. x = word_of_int y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2236
  by (rule_tac x="uint x" in exI) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2237
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2238
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2239
subsection \<open>Order on fixed-length words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2240
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2241
lemma udvd_nat_alt: "a udvd b \<longleftrightarrow> (\<exists>n\<ge>0. unat b = n * unat a)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2242
  supply nat_uint_eq [simp del]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2243
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2244
  apply safe
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2245
   apply (simp add: unat_eq_nat_uint nat_mult_distrib)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2246
  apply (simp add: uint_nat)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2247
  apply (rule exI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2248
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2249
   prefer 2
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2250
   apply (erule notE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2251
   apply (rule refl)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2252
  apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2253
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2254
61941
31f2105521ee discontinued ASCII replacement syntax <->;
wenzelm
parents: 61824
diff changeset
  2255
lemma udvd_iff_dvd: "x udvd y \<longleftrightarrow> unat x dvd unat y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2256
  unfolding dvd_def udvd_nat_alt by force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2257
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2258
lemma unat_minus_one:
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2259
  \<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
  2260
proof -
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2261
  have "0 \<le> uint w" by (fact uint_nonnegative)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2262
  moreover from that have "0 \<noteq> uint w"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2263
    by (simp add: uint_0_iff)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2264
  ultimately have "1 \<le> uint w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2265
    by arith
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2266
  from uint_lt2p [of w] have "uint w - 1 < 2 ^ LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2267
    by arith
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2268
  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
  2269
    by (auto intro: mod_pos_pos_trivial)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2270
  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
  2271
    by (auto simp del: nat_uint_eq)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2272
  then show ?thesis
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2273
    by (simp only: unat_eq_nat_uint int_word_uint word_arith_wis mod_diff_right_eq)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2274
qed
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2275
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2276
lemma measure_unat: "p \<noteq> 0 \<Longrightarrow> unat (p - 1) < unat p"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2277
  by (simp add: unat_minus_one) (simp add: unat_0_iff [symmetric])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2278
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2279
lemmas uint_add_ge0 [simp] = add_nonneg_nonneg [OF uint_ge_0 uint_ge_0]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2280
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
  2281
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2282
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
  2283
  for x :: "'a::len word" and y :: "'b::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2284
  using uint_ge_0 [of y] uint_lt2p [of x] by arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2285
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2286
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2287
subsection \<open>Conditions for the addition (etc) of two words to overflow\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2288
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2289
lemma uint_add_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2290
  "(uint x + uint y < 2 ^ LENGTH('a)) =
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2291
    (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
  2292
  for x y :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2293
  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
  2294
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2295
lemma uint_mult_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2296
  "(uint x * uint y < 2 ^ LENGTH('a)) =
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2297
    (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
  2298
  for x y :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2299
  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
  2300
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2301
lemma uint_sub_lem: "uint x \<ge> uint y \<longleftrightarrow> uint (x - y) = uint x - uint y"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2302
  by (metis (mono_tags, hide_lams) diff_ge_0_iff_ge mod_pos_pos_trivial of_nat_0_le_iff take_bit_eq_mod uint_nat uint_sub_lt2p word_sub_wi word_ubin.eq_norm)  find_theorems uint \<open>- _\<close>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2303
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2304
lemma uint_add_le: "uint (x + y) \<le> uint x + uint y"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2305
  unfolding uint_word_ariths by (simp add: zmod_le_nonneg_dividend) 
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2306
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2307
lemma uint_sub_ge: "uint (x - y) \<ge> uint x - uint y"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2308
  unfolding uint_word_ariths by (simp add: int_mod_ge)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2309
  
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2310
lemma mod_add_if_z:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2311
  "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2312
    (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
  2313
  for x y z :: int
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2314
  apply (auto simp add: not_less)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2315
  apply (rule antisym)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2316
  apply (metis diff_ge_0_iff_ge minus_mod_self2 zmod_le_nonneg_dividend)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2317
   apply (simp only: flip: minus_mod_self2 [of \<open>x + y\<close> z])
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2318
  apply (rule int_mod_ge)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2319
   apply simp_all
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2320
  done
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2321
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2322
lemma uint_plus_if':
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2323
  "uint (a + b) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2324
    (if uint a + uint b < 2 ^ LENGTH('a) then uint a + uint b
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2325
     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
  2326
  for a b :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2327
  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
  2328
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2329
lemma mod_sub_if_z:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2330
  "x < z \<Longrightarrow> y < z \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> z \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2331
    (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
  2332
  for x y z :: int
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2333
  apply (auto simp add: not_le)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2334
  apply (rule antisym)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2335
   apply (simp only: flip: mod_add_self2 [of \<open>x - y\<close> z])
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2336
   apply (rule zmod_le_nonneg_dividend)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2337
   apply simp
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  2338
  apply (metis add.commute add.right_neutral add_le_cancel_left diff_ge_0_iff_ge int_mod_ge le_less le_less_trans mod_add_self1 not_less)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2339
  done
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2340
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2341
lemma uint_sub_if':
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2342
  "uint (a - b) =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2343
    (if uint b \<le> uint a then uint a - uint b
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2344
     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
  2345
  for a b :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2346
  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
  2347
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  2348
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2349
subsection \<open>Definition of \<open>uint_arith\<close>\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2350
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2351
lemma word_of_int_inverse:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2352
  "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
  2353
  for a :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2354
  apply (erule word_uint.Abs_inverse' [rotated])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2355
  apply (simp add: uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2356
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2357
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2358
lemma uint_split:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2359
  "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
  2360
  for x :: "'a::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2361
  by transfer (auto simp add: take_bit_eq_mod take_bit_int_less_exp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2362
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2363
lemma uint_split_asm:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2364
  "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
  2365
  for x :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2366
  by (auto dest!: word_of_int_inverse
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  2367
      simp: int_word_uint
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2368
      split: uint_split)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2369
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2370
lemmas uint_splits = uint_split uint_split_asm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2371
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2372
lemmas uint_arith_simps =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2373
  word_le_def word_less_alt
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2374
  word_uint.Rep_inject [symmetric]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2375
  uint_sub_if' uint_plus_if'
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2376
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2377
\<comment> \<open>use this to stop, eg. \<open>2 ^ LENGTH(32)\<close> being simplified\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2378
lemma power_False_cong: "False \<Longrightarrow> a ^ b = c ^ d"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2379
  by auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2380
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2381
\<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
  2382
ML \<open>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2383
fun uint_arith_simpset ctxt =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2384
  ctxt addsimps @{thms uint_arith_simps}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2385
     delsimps @{thms word_uint.Rep_inject}
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  2386
     |> fold Splitter.add_split @{thms if_split_asm}
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45604
diff changeset
  2387
     |> fold Simplifier.add_cong @{thms power_False_cong}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2388
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2389
fun uint_arith_tacs ctxt =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2390
  let
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2391
    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
  2392
      Arith_Data.arith_tac ctxt n t
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2393
        handle Cooper.COOPER _ => Seq.empty;
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2394
  in
42793
88bee9f6eec7 proper Proof.context for classical tactics;
wenzelm
parents: 41550
diff changeset
  2395
    [ clarify_tac ctxt 1,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2396
      full_simp_tac (uint_arith_simpset ctxt) 1,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2397
      ALLGOALS (full_simp_tac
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2398
        (put_simpset HOL_ss ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2399
          |> fold Splitter.add_split @{thms uint_splits}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2400
          |> fold Simplifier.add_cong @{thms power_False_cong})),
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2401
      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
  2402
      ALLGOALS  (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2403
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2404
                         REPEAT (dresolve_tac ctxt @{thms word_of_int_inverse} n
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2405
                                 THEN assume_tac ctxt n
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58874
diff changeset
  2406
                                 THEN assume_tac ctxt n)),
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2407
      TRYALL arith_tac' ]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2408
  end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2409
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2410
fun uint_arith_tac ctxt = SELECT_GOAL (EVERY (uint_arith_tacs ctxt))
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2411
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2412
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2413
method_setup uint_arith =
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2414
  \<open>Scan.succeed (SIMPLE_METHOD' o uint_arith_tac)\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2415
  "solving word arithmetic via integers and arith"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2416
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2417
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2418
subsection \<open>More on overflows and monotonicity\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2419
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2420
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
  2421
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2422
  unfolding word_size by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2423
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2424
lemmas no_olen_add = no_plus_overflow_uint_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2425
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2426
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
  2427
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2428
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2429
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2430
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
  2431
  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
  2432
  by (simp add: ac_simps no_olen_add)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2433
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2434
lemmas olen_add_eqv = trans [OF no_olen_add no_olen_add' [symmetric]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2435
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2436
lemmas uint_plus_simple_iff = trans [OF no_olen_add uint_add_lem]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2437
lemmas uint_plus_simple = uint_plus_simple_iff [THEN iffD1]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2438
lemmas uint_minus_simple_iff = trans [OF no_ulen_sub uint_sub_lem]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2439
lemmas uint_minus_simple_alt = uint_sub_lem [folded word_le_def]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2440
lemmas word_sub_le_iff = no_ulen_sub [folded word_le_def]
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2441
lemmas word_sub_le = word_sub_le_iff [THEN iffD2]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2442
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2443
lemma word_less_sub1: "x \<noteq> 0 \<Longrightarrow> 1 < x \<longleftrightarrow> 0 < x - 1"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2444
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2445
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2446
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2447
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
  2448
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2449
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2450
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2451
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
  2452
  for x z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2453
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2454
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2455
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
  2456
  for x z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2457
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2458
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2459
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
  2460
  for x ab c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2461
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2462
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2463
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
  2464
  for x ab c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2465
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2466
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2467
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
  2468
  for a b c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2469
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2470
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2471
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
  2472
  for a b c :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2473
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2474
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2475
lemmas le_plus = le_plus' [rotated]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2476
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2477
lemmas le_minus = leD [THEN thin_rl, THEN le_minus'] (* FIXME *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2478
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2479
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
  2480
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2481
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2482
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2483
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
  2484
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2485
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2486
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2487
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
  2488
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2489
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2490
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2491
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
  2492
  for a b c d :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2493
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2494
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2495
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
  2496
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2497
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2498
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2499
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
  2500
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2501
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2502
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2503
lemma word_le_minus_mono:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2504
  "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
  2505
  for a b c d :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2506
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2507
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2508
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
  2509
  for x y y' :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2510
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2511
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2512
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
  2513
  for x y y' :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2514
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2515
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2516
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
  2517
  for a b c :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2518
  by uint_arith
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2519
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2520
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
  2521
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2522
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2523
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2524
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
  2525
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2526
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2527
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2528
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
  2529
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2530
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2531
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2532
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
  2533
  for x z k :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2534
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2535
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2536
lemma inc_le: "i < m \<Longrightarrow> i + 1 \<le> m"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2537
  for i m :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2538
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2539
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2540
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
  2541
  for i m :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2542
  by uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2543
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2544
lemma udvd_incr_lem:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2545
  "up < uq \<Longrightarrow> up = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2546
    uq = ua + n' * uint K \<Longrightarrow> up + uint K \<le> uq"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2547
  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
  2548
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2549
lemma udvd_incr':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2550
  "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2551
    uint q = ua + n' * uint K \<Longrightarrow> p + K \<le> q"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2552
  apply (unfold word_less_alt word_le_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2553
  apply (drule (2) udvd_incr_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2554
  apply (erule uint_add_le [THEN order_trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2555
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2556
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2557
lemma udvd_decr':
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2558
  "p < q \<Longrightarrow> uint p = ua + n * uint K \<Longrightarrow>
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2559
    uint q = ua + n' * uint K \<Longrightarrow> p \<le> q - K"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2560
  apply (unfold word_less_alt word_le_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2561
  apply (drule (2) udvd_incr_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2562
  apply (drule le_diff_eq [THEN iffD2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2563
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2564
  apply (rule uint_sub_ge)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2565
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2566
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2567
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
  2568
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
  2569
lemmas udvd_decr0 = udvd_decr' [where ua=0, unfolded add_0_left]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2570
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2571
lemma udvd_minus_le': "xy < k \<Longrightarrow> z udvd xy \<Longrightarrow> z udvd k \<Longrightarrow> xy \<le> k - z"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2572
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2573
  apply clarify
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2574
  apply (erule (2) udvd_decr0)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2575
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2576
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2577
lemma udvd_incr2_K:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2578
  "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
  2579
    0 < K \<Longrightarrow> p \<le> p + K \<and> p + K \<le> a + s"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2580
  supply [[simproc del: linordered_ring_less_cancel_factor]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2581
  apply (unfold udvd_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2582
  apply clarify
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  2583
  apply (simp add: uint_arith_simps split: if_split_asm)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2584
   prefer 2
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2585
   apply (insert uint_range' [of s])[1]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2586
   apply arith
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2587
  apply (drule add.commute [THEN xtrans(1)])
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2588
  apply (simp flip: diff_less_eq)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2589
  apply (subst (asm) mult_less_cancel_right)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2590
  apply simp
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2591
  apply (simp add: diff_eq_eq not_less)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2592
  apply (subst (asm) (3) zless_iff_Suc_zadd)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2593
  apply auto
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2594
    apply (auto simp add: algebra_simps)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2595
  apply (drule less_le_trans [of _ \<open>2 ^ LENGTH('a)\<close>]) apply assumption
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2596
  apply (simp add: mult_less_0_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2597
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2598
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2599
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2600
subsection \<open>Arithmetic type class instantiations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2601
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2602
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
  2603
  word_zero_le [THEN leD, THEN antisym_conv1]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2604
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2605
lemma word_of_int_nat: "0 \<le> x \<Longrightarrow> word_of_int x = of_nat (nat x)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2606
  by (simp add: word_of_int)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2607
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2608
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2609
  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
  2610
  which requires word length \<open>\<ge> 1\<close>, ie \<open>'a::len word\<close>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2611
\<close>
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  2612
lemma iszero_word_no [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2613
  "iszero (numeral bin :: 'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  2614
    iszero (take_bit LENGTH('a) (numeral bin :: int))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2615
  using word_ubin.norm_eq_iff [where 'a='a, of "numeral bin" 0]
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  2616
  by (simp add: iszero_def [symmetric])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2617
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2618
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
  2619
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2620
lemmas word_eq_numeral_iff_iszero [simp] =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2621
  eq_numeral_iff_iszero [where 'a="'a::len word"]
46603
83a5160e6b4d removed unnecessary lemma zero_bintrunc
huffman
parents: 46602
diff changeset
  2622
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2623
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2624
subsection \<open>Word and nat\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2625
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2626
lemma word_nchotomy: "\<forall>w :: 'a::len word. \<exists>n. w = of_nat n \<and> n < 2 ^ LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2627
  apply (rule allI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2628
  apply (rule word_unat.Abs_cases)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2629
  apply (unfold unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2630
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2631
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2632
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2633
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
  2634
  for w :: "'a::len word"
68157
057d5b4ce47e removed some non-essential rules
haftmann
parents: 67443
diff changeset
  2635
  using mod_div_mult_eq [of n "2 ^ LENGTH('a)", symmetric]
057d5b4ce47e removed some non-essential rules
haftmann
parents: 67443
diff changeset
  2636
  by (auto simp add: word_unat.inverse_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2637
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2638
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
  2639
  unfolding word_size by (rule of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2640
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2641
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
  2642
  by (simp add: of_nat_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2643
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2644
lemma of_nat_2p [simp]: "of_nat (2 ^ LENGTH('a)) = (0::'a::len word)"
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  2645
  by (fact mult_1 [symmetric, THEN iffD2 [OF of_nat_0 exI]])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2646
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2647
lemma of_nat_gt_0: "of_nat k \<noteq> 0 \<Longrightarrow> 0 < k"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2648
  by (cases k) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2649
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2650
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
  2651
  by (auto simp add : of_nat_0)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2652
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2653
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
  2654
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2655
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2656
lemma Abs_fnat_hom_mult: "of_nat a * of_nat b = (of_nat (a * b) :: 'a::len word)"
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61424
diff changeset
  2657
  by (simp add: word_of_nat wi_hom_mult)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2658
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2659
lemma Abs_fnat_hom_Suc: "word_succ (of_nat a) = of_nat (Suc a)"
57514
bdc2c6b40bf2 prefer ac_simps collections over separate name bindings for add and mult
haftmann
parents: 57512
diff changeset
  2660
  by (simp add: word_of_nat wi_hom_succ ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2661
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2662
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
  2663
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2664
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2665
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
  2666
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2667
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2668
lemmas Abs_fnat_homs =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2669
  Abs_fnat_hom_add Abs_fnat_hom_mult Abs_fnat_hom_Suc
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2670
  Abs_fnat_hom_0 Abs_fnat_hom_1
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2671
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2672
lemma word_arith_nat_add: "a + b = of_nat (unat a + unat b)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2673
  by simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2674
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2675
lemma word_arith_nat_mult: "a * b = of_nat (unat a * unat b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2676
  by simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2677
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2678
lemma word_arith_nat_Suc: "word_succ a = of_nat (Suc (unat a))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2679
  by (subst Abs_fnat_hom_Suc [symmetric]) simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2680
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2681
lemma word_arith_nat_div: "a div b = of_nat (unat a div unat b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2682
  by (simp add: word_div_def word_of_nat zdiv_int uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2683
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2684
lemma word_arith_nat_mod: "a mod b = of_nat (unat a mod unat b)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2685
  by (simp add: word_mod_def word_of_nat zmod_int uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2686
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2687
lemmas word_arith_nat_defs =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2688
  word_arith_nat_add word_arith_nat_mult
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2689
  word_arith_nat_Suc Abs_fnat_hom_0
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2690
  Abs_fnat_hom_1 word_arith_nat_div
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2691
  word_arith_nat_mod
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2692
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2693
lemma unat_cong: "x = y \<Longrightarrow> unat x = unat y"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2694
  by simp
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2695
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2696
lemmas unat_word_ariths = word_arith_nat_defs
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2697
  [THEN trans [OF unat_cong unat_of_nat]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2698
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2699
lemmas word_sub_less_iff = word_sub_le_iff
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  2700
  [unfolded linorder_not_less [symmetric] Not_eq_iff]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2701
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2702
lemma unat_add_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2703
  "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
  2704
  for x y :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2705
  apply (auto simp: unat_word_ariths)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2706
  apply (metis unat_lt2p word_unat.eq_norm)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2707
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2708
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2709
lemma unat_mult_lem:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2710
  "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
  2711
  for x y :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2712
  apply (auto simp: unat_word_ariths)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2713
  apply (metis unat_lt2p word_unat.eq_norm)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2714
  done
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2715
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2716
lemma unat_plus_if':
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2717
  \<open>unat (a + b) =
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2718
    (if unat a + unat b < 2 ^ LENGTH('a)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2719
    then unat a + unat b
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2720
    else unat a + unat b - 2 ^ LENGTH('a))\<close> for a b :: \<open>'a::len word\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2721
  apply (auto simp: unat_word_ariths not_less)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2722
  apply (subst (asm) le_iff_add)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2723
  apply auto
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2724
  apply (metis add_less_cancel_left add_less_cancel_right le_less_trans less_imp_le mod_less unat_lt2p)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2725
  done
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2726
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2727
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
  2728
  for a b x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2729
  apply (erule order_trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2730
  apply (erule olen_add_eqv [THEN iffD1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2731
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2732
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2733
lemmas un_ui_le =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2734
  trans [OF word_le_nat_alt [symmetric] word_le_def]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2735
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2736
lemma unat_sub_if_size:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2737
  "unat (x - y) =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2738
    (if unat y \<le> unat x
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2739
     then unat x - unat y
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2740
     else unat x + 2 ^ size x - unat y)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2741
  supply nat_uint_eq [simp del]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2742
  apply (unfold word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2743
  apply (simp add: un_ui_le)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2744
  apply (auto simp add: unat_eq_nat_uint uint_sub_if')
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2745
   apply (rule nat_diff_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2746
    prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2747
    apply (simp add: algebra_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2748
    apply (rule nat_diff_distrib [THEN trans])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2749
      prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2750
      apply (subst nat_add_distrib)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2751
        prefer 3
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2752
        apply (simp add: nat_power_eq)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2753
       apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2754
  apply uint_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2755
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2756
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2757
lemmas unat_sub_if' = unat_sub_if_size [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2758
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2759
lemma uint_div:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2760
  \<open>uint (x div y) = uint x div uint y\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2761
  by (metis div_le_dividend le_less_trans mod_less uint_nat unat_lt2p unat_word_ariths(6) zdiv_int)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2762
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2763
lemma unat_div:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2764
  \<open>unat (x div y) = unat x div unat y\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2765
  by (simp add: uint_div nat_div_distrib flip: nat_uint_eq)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2766
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2767
lemma uint_mod:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2768
  \<open>uint (x mod y) = uint x mod uint y\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2769
  by (metis (no_types, lifting) le_less_trans mod_by_0 mod_le_divisor mod_less neq0_conv uint_nat unat_lt2p unat_word_ariths(7) zmod_int)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2770
  
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2771
lemma unat_mod:
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2772
  \<open>unat (x mod y) = unat x mod unat y\<close>
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  2773
  by (simp add: uint_mod nat_mod_distrib flip: nat_uint_eq)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2774
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2775
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2776
text \<open>Definition of \<open>unat_arith\<close> tactic\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2777
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2778
lemma unat_split: "P (unat x) \<longleftrightarrow> (\<forall>n. of_nat n = x \<and> n < 2^LENGTH('a) \<longrightarrow> P n)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2779
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2780
  by (auto simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2781
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2782
lemma unat_split_asm: "P (unat x) \<longleftrightarrow> (\<nexists>n. of_nat n = x \<and> n < 2^LENGTH('a) \<and> \<not> P n)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2783
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2784
  by (auto simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2785
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2786
lemmas of_nat_inverse =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2787
  word_unat.Abs_inverse' [rotated, unfolded unats_def, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2788
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2789
lemmas unat_splits = unat_split unat_split_asm
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2790
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2791
lemmas unat_arith_simps =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2792
  word_le_nat_alt word_less_nat_alt
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2793
  word_unat.Rep_inject [symmetric]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2794
  unat_sub_if' unat_plus_if' unat_div unat_mod
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2795
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2796
\<comment> \<open>\<open>unat_arith_tac\<close>: tactic to reduce word arithmetic to \<open>nat\<close>, try to solve via \<open>arith\<close>\<close>
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2797
ML \<open>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2798
fun unat_arith_simpset ctxt =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2799
  ctxt addsimps @{thms unat_arith_simps}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2800
     delsimps @{thms word_unat.Rep_inject}
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  2801
     |> fold Splitter.add_split @{thms if_split_asm}
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45604
diff changeset
  2802
     |> fold Simplifier.add_cong @{thms power_False_cong}
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2803
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2804
fun unat_arith_tacs ctxt =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2805
  let
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2806
    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
  2807
      Arith_Data.arith_tac ctxt n t
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2808
        handle Cooper.COOPER _ => Seq.empty;
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2809
  in
42793
88bee9f6eec7 proper Proof.context for classical tactics;
wenzelm
parents: 41550
diff changeset
  2810
    [ clarify_tac ctxt 1,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2811
      full_simp_tac (unat_arith_simpset ctxt) 1,
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2812
      ALLGOALS (full_simp_tac
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2813
        (put_simpset HOL_ss ctxt
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2814
          |> fold Splitter.add_split @{thms unat_splits}
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 51375
diff changeset
  2815
          |> fold Simplifier.add_cong @{thms power_False_cong})),
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2816
      rewrite_goals_tac ctxt @{thms word_size},
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2817
      ALLGOALS (fn n => REPEAT (resolve_tac ctxt [allI, impI] n) THEN
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2818
                         REPEAT (eresolve_tac ctxt [conjE] n) THEN
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 60429
diff changeset
  2819
                         REPEAT (dresolve_tac ctxt @{thms of_nat_inverse} n THEN assume_tac ctxt n)),
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2820
      TRYALL arith_tac' ]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2821
  end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2822
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2823
fun unat_arith_tac ctxt = SELECT_GOAL (EVERY (unat_arith_tacs ctxt))
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2824
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2825
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2826
method_setup unat_arith =
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2827
  \<open>Scan.succeed (SIMPLE_METHOD' o unat_arith_tac)\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2828
  "solving word arithmetic via natural numbers and arith"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2829
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2830
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
  2831
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2832
  unfolding word_size by unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2833
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2834
lemmas no_olen_add_nat =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2835
  no_plus_overflow_unat_size [unfolded word_size]
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2836
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2837
lemmas unat_plus_simple =
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2838
  trans [OF no_olen_add_nat unat_add_lem]
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2839
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2840
lemma word_div_mult: "0 < y \<Longrightarrow> unat x * unat y < 2 ^ LENGTH('a) \<Longrightarrow> x * y div y = x"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2841
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2842
  apply unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2843
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2844
  apply (subst unat_mult_lem [THEN iffD1])
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2845
   apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2846
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2847
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2848
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
  2849
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2850
  apply unat_arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2851
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2852
  apply (drule mult_le_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2853
  apply (erule order_le_less_trans)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2854
  apply (metis add_lessD1 div_mult_mod_eq unat_lt2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2855
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2856
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2857
lemmas div_lt'' = order_less_imp_le [THEN div_lt']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2858
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2859
lemma div_lt_mult: "i < k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x < k"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2860
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2861
  apply (frule div_lt'' [THEN unat_mult_lem [THEN iffD1]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2862
  apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2863
  apply (drule (1) mult_less_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2864
  apply (erule order_less_le_trans)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2865
  apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2866
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2867
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2868
lemma div_le_mult: "i \<le> k div x \<Longrightarrow> 0 < x \<Longrightarrow> i * x \<le> k"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2869
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2870
  apply (frule div_lt' [THEN unat_mult_lem [THEN iffD1]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2871
  apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2872
  apply (drule mult_le_mono1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2873
  apply (erule order_trans)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2874
  apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2875
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2876
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2877
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
  2878
  for i k x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2879
  apply (unfold uint_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2880
  apply (drule div_lt')
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2881
  apply (metis of_nat_less_iff of_nat_mult of_nat_numeral of_nat_power)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2882
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2883
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2884
lemmas div_lt_uint'' = order_less_imp_le [THEN div_lt_uint']
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2885
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  2886
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
  2887
  for x y z :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2888
  by (metis add_diff_cancel_left' add_diff_eq uint_add_lem uint_plus_simple)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2889
  
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2890
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
  2891
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2892
lemmas plus_minus_no_overflow =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2893
  order_less_imp_le [THEN plus_minus_no_overflow_ab]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2894
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2895
lemmas mcs = word_less_minus_cancel word_less_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2896
  word_le_minus_cancel word_le_minus_mono_left
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2897
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2898
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
  2899
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
  2900
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
  2901
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2902
lemmas le_unat_uoi = unat_le [THEN word_unat.Abs_inverse]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2903
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66453
diff changeset
  2904
lemmas thd = times_div_less_eq_dividend
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2905
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2906
lemmas uno_simps [THEN le_unat_uoi] = mod_le_divisor div_le_dividend
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2907
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2908
lemma word_mod_div_equality: "(n div b) * b + (n mod b) = n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2909
  for n b :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2910
  by (fact div_mult_mod_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2911
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2912
lemma word_div_mult_le: "a div b * b \<le> a"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2913
  for a b :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2914
  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
  2915
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2916
lemma word_mod_less_divisor: "0 < n \<Longrightarrow> m mod n < n"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2917
  for m n :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2918
  by (simp add: unat_arith_simps)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  2919
  
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2920
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
  2921
  by (induct n) (simp_all add: wi_hom_mult [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2922
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2923
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
  2924
  by (simp add : word_of_int_power_hom [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2925
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2926
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
  2927
  for n :: "'a::len word"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2928
  by unat_arith
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2929
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2930
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2931
subsection \<open>Cardinality, finiteness of set of words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2932
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
  2933
lemma inj_on_word_of_int: \<open>inj_on (word_of_int :: int \<Rightarrow> 'a word) {0..<2 ^ LENGTH('a::len)}\<close>
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2934
  apply (rule inj_onI)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2935
  apply transfer
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2936
  apply (simp add: take_bit_eq_mod)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2937
  done
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2938
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2939
lemma inj_uint: \<open>inj uint\<close>
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2940
  by (rule injI) simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2941
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
  2942
lemma range_uint: \<open>range (uint :: 'a word \<Rightarrow> int) = {0..<2 ^ LENGTH('a::len)}\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2943
  by transfer (auto simp add: bintr_lt2p range_bintrunc)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2944
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
  2945
lemma UNIV_eq: \<open>(UNIV :: 'a word set) = word_of_int ` {0..<2 ^ LENGTH('a::len)}\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2946
proof -
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
  2947
  have \<open>uint ` (UNIV :: 'a word set) = uint ` (word_of_int :: int \<Rightarrow> 'a word) ` {0..<2 ^ LENGTH('a::len)}\<close>
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  2948
    by transfer (simp add: range_bintrunc, auto simp add: take_bit_eq_mod)
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2949
  then show ?thesis
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2950
    using inj_image_eq_iff [of \<open>uint :: 'a word \<Rightarrow> int\<close> \<open>UNIV :: 'a word set\<close> \<open>word_of_int ` {0..<2 ^ LENGTH('a)} :: 'a word set\<close>, OF inj_uint]
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2951
    by simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2952
qed
45809
2bee94cbae72 finite class instance for word type; remove unused lemmas
huffman
parents: 45808
diff changeset
  2953
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
  2954
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
  2955
  by (simp add: UNIV_eq card_image inj_on_word_of_int)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2956
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  2957
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
  2958
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  2959
  unfolding word_size by (rule card_word)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2960
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
  2961
instance word :: (len) finite
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2962
  by standard (simp add: UNIV_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
  2963
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2964
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  2965
subsection \<open>Bitwise Operations on Words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2966
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2967
lemmas bin_log_bintrs = bin_trunc_not bin_trunc_xor bin_trunc_and bin_trunc_or
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2968
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2969
\<comment> \<open>following definitions require both arithmetic and bit-wise word operations\<close>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2970
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2971
\<comment> \<open>to get \<open>word_no_log_defs\<close> from \<open>word_log_defs\<close>, using \<open>bin_log_bintrs\<close>\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2972
lemmas wils1 = bin_log_bintrs [THEN word_ubin.norm_eq_iff [THEN iffD1],
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  2973
  folded word_ubin.eq_norm, THEN eq_reflection]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2974
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  2975
\<comment> \<open>the binary operations only\<close>  (* BH: why is this needed? *)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  2976
lemmas word_log_binary_defs =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2977
  word_and_def word_or_def word_xor_def
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  2978
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2979
lemma word_wi_log_defs:
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  2980
  "NOT (word_of_int a) = word_of_int (NOT a)"
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2981
  "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
  2982
  "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
  2983
  "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
  2984
  by (transfer, rule refl)+
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  2985
46011
96a5f44c22da replace 'lemmas' with explicit 'lemma'
huffman
parents: 46010
diff changeset
  2986
lemma word_no_log_defs [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2987
  "NOT (numeral a) = word_of_int (NOT (numeral a))"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  2988
  "NOT (- numeral a) = word_of_int (NOT (- numeral a))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  2989
  "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
  2990
  "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
  2991
  "- 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
  2992
  "- 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
  2993
  "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
  2994
  "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
  2995
  "- 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
  2996
  "- 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
  2997
  "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
  2998
  "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
  2999
  "- 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
  3000
  "- 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
  3001
  by (transfer, rule refl)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3002
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3003
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
  3004
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  3005
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
  3006
  "NOT (1::'a::len word) = -2"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3007
  "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
  3008
  "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
  3009
  "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
  3010
  "- 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
  3011
  "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
  3012
  "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
  3013
  "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
  3014
  "- 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
  3015
  "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
  3016
  "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
  3017
  "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
  3018
  "- numeral a XOR 1 = word_of_int (- numeral a XOR 1)"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3019
  by (transfer, simp)+
46064
88ef116e0522 add simp rules for bitwise word operations with 1
huffman
parents: 46057
diff changeset
  3020
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3021
text \<open>Special cases for when one of the arguments equals -1.\<close>
56979
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3022
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3023
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
  3024
  "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
  3025
  "(-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
  3026
  "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
  3027
  "(-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
  3028
  "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
  3029
  " (-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
  3030
  "x XOR (-1::'a::len word) = NOT x"
56979
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3031
  by (transfer, simp)+
376604d56b54 added lemmas for -1
noschinl
parents: 56078
diff changeset
  3032
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3033
lemma uint_and:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3034
  \<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
  3035
  by transfer simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3036
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3037
lemma uint_or:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3038
  \<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
  3039
  by transfer simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3040
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3041
lemma uint_xor:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3042
  \<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
  3043
  by transfer simp
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3044
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3045
lemma test_bit_wi [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
  3046
  "(word_of_int x :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a) \<and> bin_nth x n"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3047
  by (simp add: word_test_bit_def word_ubin.eq_norm nth_bintr)
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3048
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3049
lemma word_test_bit_transfer [transfer_rule]:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3050
  "(rel_fun pcr_word (rel_fun (=) (=)))
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3051
    (\<lambda>x n. n < LENGTH('a) \<and> bit x n) (test_bit :: 'a::len word \<Rightarrow> _)"
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3052
  by (simp only: test_bit_eq_bit) transfer_prover
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3053
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3054
lemma word_ops_nth_size:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3055
  "n < size x \<Longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3056
    (x OR y) !! n = (x !! n | y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3057
    (x AND y) !! n = (x !! n \<and> y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3058
    (x XOR y) !! n = (x !! n \<noteq> y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3059
    (NOT x) !! n = (\<not> x !! 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
  3060
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3061
  unfolding word_size by transfer (simp add: bin_nth_ops)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3062
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3063
lemma word_ao_nth:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3064
  "(x OR y) !! n = (x !! n | y !! n) \<and>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3065
    (x AND y) !! n = (x !! n \<and> y !! 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
  3066
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3067
  by transfer (auto simp add: bin_nth_ops)
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3068
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3069
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN word_ops_nth_size [unfolded word_size]]
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3070
lemmas msb1 = msb0 [where i = 0]
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3071
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3072
lemma test_bit_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
  3073
  "(numeral w :: 'a::len word) !! n \<longleftrightarrow>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3074
    n < LENGTH('a) \<and> bin_nth (numeral w) n"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3075
  by transfer (rule refl)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3076
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3077
lemma test_bit_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
  3078
  "(- numeral w :: 'a::len word) !! n \<longleftrightarrow>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3079
    n < LENGTH('a) \<and> bin_nth (- numeral w) n"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3080
  by transfer (rule refl)
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3081
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3082
lemma test_bit_1 [simp]: "(1 :: 'a::len word) !! n \<longleftrightarrow> n = 0"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3083
  by transfer auto
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3084
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
  3085
lemma nth_0 [simp]: "\<not> (0 :: 'a::len word) !! n"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3086
  by transfer simp
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3087
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
  3088
lemma nth_minus1 [simp]: "(-1 :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3089
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3090
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3091
\<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
  3092
lemmas bwsimps =
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
  3093
  wi_hom_add
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3094
  word_wi_log_defs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3095
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3096
lemma word_bw_assocs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3097
  "(x AND y) AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3098
  "(x OR y) OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3099
  "(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
  3100
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  3101
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3102
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3103
lemma word_bw_comms:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3104
  "x AND y = y AND x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3105
  "x OR y = y OR x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3106
  "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
  3107
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  3108
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3109
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3110
lemma word_bw_lcs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3111
  "y AND x AND z = x AND y AND z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3112
  "y OR x OR z = x OR y OR z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3113
  "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
  3114
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  3115
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3116
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3117
lemma word_log_esimps:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3118
  "x AND 0 = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3119
  "x AND -1 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3120
  "x OR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3121
  "x OR -1 = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3122
  "x XOR 0 = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3123
  "x XOR -1 = NOT x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3124
  "0 AND x = 0"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3125
  "-1 AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3126
  "0 OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3127
  "-1 OR x = -1"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3128
  "0 XOR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3129
  "-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
  3130
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3131
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3132
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3133
lemma word_not_dist:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3134
  "NOT (x OR y) = NOT x AND NOT y"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3135
  "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
  3136
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3137
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3138
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3139
lemma word_bw_same:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3140
  "x AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3141
  "x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3142
  "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
  3143
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3144
  by simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3145
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3146
lemma word_ao_absorbs [simp]:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3147
  "x AND (y OR x) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3148
  "x OR y AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3149
  "x AND (x OR y) = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3150
  "y AND x OR x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3151
  "(y OR x) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3152
  "x OR x AND y = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3153
  "(x OR y) AND x = x"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3154
  "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
  3155
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  3156
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3157
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  3158
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
  3159
  for x :: "'a::len word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3160
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3161
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3162
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
  3163
  for x :: "'a::len word"
46022
657f87b10944 simplify some proofs
huffman
parents: 46021
diff changeset
  3164
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
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 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
  3167
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3168
  by (auto simp: word_eq_iff word_ops_nth_size [unfolded word_size])
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3169
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3170
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
  3171
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3172
  by transfer (simp add: bin_add_not)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3173
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3174
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
  3175
  for x :: "'a::len word"
47372
9ab4e22dac7b configure transfer method for 'a word -> int
huffman
parents: 47168
diff changeset
  3176
  by transfer (simp add: plus_and_or)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3177
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3178
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
  3179
  for x :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3180
  by auto
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3181
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3182
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
  3183
  for x' :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3184
  by auto
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3185
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3186
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
  3187
  for w w' :: "'a::len word"
48196
b7313810b6e6 explicit is better than implicit;
wenzelm
parents: 47941
diff changeset
  3188
  by (auto intro: leoa leao)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3189
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3190
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
  3191
  for x y :: "'a::len word"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3192
  by (auto simp: word_le_def uint_or intro: le_int_or)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3193
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3194
lemmas le_word_or1 = xtrans(3) [OF word_bw_comms (2) le_word_or2]
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3195
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
  3196
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
  3197
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3198
lemma bit_horner_sum_bit_word_iff:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3199
  \<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
  3200
    \<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
  3201
  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
  3202
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3203
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
  3204
  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
  3205
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3206
lemma bit_word_reverse_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3207
  \<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
  3208
  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
  3209
  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
  3210
    (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
  3211
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3212
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
  3213
  by (rule bit_word_eqI)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3214
    (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
  3215
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3216
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
  3217
  by (metis word_rev_rev)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3218
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3219
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
  3220
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3221
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3222
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size]]
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3223
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3224
lemma nth_sint:
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3225
  fixes w :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3226
  defines "l \<equiv> LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3227
  shows "bin_nth (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3228
  unfolding sint_uint l_def
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3229
  by (auto simp: nth_sbintr word_test_bit_def [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3230
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3231
lemma setBit_no [simp]: "setBit (numeral bin) n = word_of_int (bin_sc n True (numeral bin))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3232
  by transfer (simp add: bin_sc_eq)
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3233
 
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  3234
lemma clearBit_no [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54743
diff changeset
  3235
  "clearBit (numeral bin) n = word_of_int (bin_sc n False (numeral bin))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3236
  by transfer (simp add: bin_sc_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3237
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3238
lemma test_bit_2p: "(word_of_int (2 ^ n)::'a::len word) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3239
  by (auto simp: word_test_bit_def word_ubin.eq_norm nth_bintr nth_2p_bin)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3240
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3241
lemma nth_w2p: "((2::'a::len word) ^ n) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a::len)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3242
  by (simp add: test_bit_2p [symmetric] word_of_int [symmetric])
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3243
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3244
lemma uint_2p: "(0::'a::len word) < 2 ^ n \<Longrightarrow> uint (2 ^ n::'a::len word) = 2 ^ n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3245
  apply (unfold word_arith_power_alt)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3246
  apply (case_tac "LENGTH('a)")
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3247
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3248
  apply (case_tac "nat")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3249
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3250
   apply (case_tac "n")
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3251
    apply clarsimp
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3252
   apply clarsimp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3253
  apply (drule word_gt_0 [THEN iffD1])
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3254
  apply (safe intro!: word_eqI)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3255
   apply (auto simp add: nth_2p_bin)
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3256
  apply (erule notE)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3257
  apply (simp (no_asm_use) add: uint_word_of_int word_size)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54225
diff changeset
  3258
  apply (subst mod_pos_pos_trivial)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3259
    apply simp
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3260
   apply (rule power_strict_increasing)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3261
    apply simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3262
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3263
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3264
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
  3265
  by (induct n) (simp_all add: wi_hom_syms)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3266
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3267
lemma bang_is_le: "x !! m \<Longrightarrow> 2 ^ m \<le> x"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3268
  for x :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3269
  apply (rule xtrans(3))
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3270
   apply (rule_tac [2] y = "x" in le_word_or2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3271
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3272
  apply (auto simp add: word_ao_nth nth_w2p word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3273
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3274
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3275
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3276
subsection \<open>Bit comprehension\<close>
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3277
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
  3278
instantiation word :: (len) bit_comprehension
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3279
begin
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3280
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3281
definition word_set_bits_def:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3282
  \<open>(BITS n. P n) = (horner_sum of_bool 2 (map P [0..<LENGTH('a)]) :: 'a word)\<close>
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3283
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3284
instance ..
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3285
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3286
end
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3287
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3288
lemma bit_set_bits_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3289
  \<open>bit (set_bits P :: 'a::len word) n \<longleftrightarrow> n < LENGTH('a) \<and> P n\<close>
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3290
  by (auto simp add: word_set_bits_def bit_horner_sum_bit_word_iff)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3291
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3292
lemma set_bits_bit_eq:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3293
  \<open>set_bits (bit w) = w\<close> 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
  3294
  by (rule bit_word_eqI) (auto simp add: bit_set_bits_word_iff bit_imp_le_length)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3295
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3296
lemma set_bits_K_False [simp]:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3297
  \<open>set_bits (\<lambda>_. False) = (0 :: 'a :: len word)\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3298
  by (rule bit_word_eqI) (simp add: bit_set_bits_word_iff)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3299
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3300
interpretation test_bit:
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3301
  td_ext
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3302
    "(!!) :: 'a::len word \<Rightarrow> nat \<Rightarrow> bool"
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3303
    set_bits
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
  3304
    "{f. \<forall>i. f i \<longrightarrow> i < LENGTH('a::len)}"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
  3305
    "(\<lambda>h i. h i \<and> i < LENGTH('a::len))"
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3306
  by standard
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3307
    (auto simp add: test_bit_word_eq bit_imp_le_length bit_set_bits_word_iff set_bits_bit_eq)
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3308
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3309
lemmas td_nth = test_bit.td_thm
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3310
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
  3311
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3312
subsection \<open>Shifting, Rotating, and Splitting Words\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3313
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3314
lemma shiftl1_wi [simp]: "shiftl1 (word_of_int w) = word_of_int (2 * w)"
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3315
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3316
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3317
lemma shiftl1_numeral [simp]: "shiftl1 (numeral w) = numeral (Num.Bit0 w)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3318
  unfolding word_numeral_alt shiftl1_wi by simp
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3319
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3320
lemma shiftl1_neg_numeral [simp]: "shiftl1 (- numeral w) = - numeral (Num.Bit0 w)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3321
  unfolding word_neg_numeral_alt shiftl1_wi by simp
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3322
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3323
lemma shiftl1_0 [simp] : "shiftl1 0 = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3324
  by transfer simp
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3325
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3326
lemma shiftl1_def_u: "shiftl1 w = word_of_int (2 * uint w)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3327
  by (fact shiftl1_eq)
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3328
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
  3329
lemma shiftl1_def_s: "shiftl1 w = word_of_int (2 * sint w)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3330
  by (simp add: shiftl1_def_u wi_hom_syms)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3331
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3332
lemma shiftr1_0 [simp]: "shiftr1 0 = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3333
  by transfer simp
45995
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3334
b16070689726 declare word_of_int_{0,1} [simp], for consistency with word_of_int_bin
huffman
parents: 45958
diff changeset
  3335
lemma sshiftr1_0 [simp]: "sshiftr1 0 = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3336
  by transfer simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3337
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3338
lemma sshiftr1_n1 [simp]: "sshiftr1 (- 1) = - 1"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3339
  by transfer simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3340
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
  3341
lemma shiftl_0 [simp]: "(0::'a::len word) << n = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3342
  by transfer simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3343
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
  3344
lemma shiftr_0 [simp]: "(0::'a::len word) >> n = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3345
  by transfer simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3346
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3347
lemma sshiftr_0 [simp]: "0 >>> n = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3348
  by transfer simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3349
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3350
lemma sshiftr_n1 [simp]: "-1 >>> n = -1"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3351
  by transfer simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3352
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3353
lemma nth_shiftl1: "shiftl1 w !! n \<longleftrightarrow> n < size w \<and> n > 0 \<and> w !! (n - 1)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3354
  by transfer (auto simp add: bit_double_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3355
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3356
lemma nth_shiftl': "(w << m) !! n \<longleftrightarrow> n < size w \<and> n >= m \<and> w !! (n - m)"
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
  3357
  for w :: "'a::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3358
  by transfer (auto simp add: bit_push_bit_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3359
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3360
lemmas nth_shiftl = nth_shiftl' [unfolded word_size]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3361
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3362
lemma nth_shiftr1: "shiftr1 w !! n = w !! Suc n"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3363
  by transfer (auto simp add: bit_take_bit_iff simp flip: bit_Suc)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3364
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3365
lemma nth_shiftr: "(w >> m) !! n = w !! (n + m)"
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
  3366
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3367
  apply (unfold shiftr_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3368
  apply (induct "m" arbitrary: n)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3369
   apply (auto simp add: nth_shiftr1)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3370
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3371
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3372
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3373
  see paper page 10, (1), (2), \<open>shiftr1_def\<close> is of the form of (1),
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3374
  where \<open>f\<close> (ie \<open>bin_rest\<close>) takes normal arguments to normal results,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3375
  thus we get (2) from (1)
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3376
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3377
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3378
lemma uint_shiftr1: "uint (shiftr1 w) = bin_rest (uint w)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3379
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3380
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3381
lemma bit_sshiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3382
  \<open>bit (sshiftr1 w) n \<longleftrightarrow> bit w (if n = LENGTH('a) - 1 then LENGTH('a) - 1 else Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3383
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3384
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3385
  apply (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def simp flip: bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3386
  using le_less_Suc_eq apply fastforce
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3387
  using le_less_Suc_eq apply fastforce
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3388
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3389
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3390
lemma bit_sshiftr_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3391
  \<open>bit (w >>> m) n \<longleftrightarrow> bit w (if LENGTH('a) - m \<le> n \<and> n < LENGTH('a) then LENGTH('a) - 1 else (m + n))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3392
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3393
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3394
  apply (auto simp add: bit_take_bit_iff bit_drop_bit_eq bit_signed_take_bit_iff min_def not_le simp flip: bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3395
  using le_less_Suc_eq apply fastforce
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3396
  using le_less_Suc_eq apply fastforce
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3397
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3398
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3399
lemma nth_sshiftr1: "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3400
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3401
  apply (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def simp flip: bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3402
  using le_less_Suc_eq apply fastforce
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3403
  using le_less_Suc_eq apply fastforce
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3404
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3405
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3406
lemma nth_sshiftr :
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3407
  "sshiftr w m !! n =
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3408
    (n < size w \<and> (if n + m \<ge> size w then w !! (size w - 1) else w !! (n + m)))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3409
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3410
  apply (auto simp add: bit_take_bit_iff bit_drop_bit_eq bit_signed_take_bit_iff min_def not_le ac_simps)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3411
  using le_less_Suc_eq apply fastforce
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3412
  using le_less_Suc_eq apply fastforce
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3413
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3414
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3415
lemma shiftr1_div_2: "uint (shiftr1 w) = uint w div 2"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3416
  by (fact uint_shiftr1)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3417
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3418
lemma sshiftr1_div_2: "sint (sshiftr1 w) = sint w div 2"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3419
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3420
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3421
lemma shiftr_div_2n: "uint (shiftr w n) = uint w div 2 ^ n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3422
  apply (unfold shiftr_def)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3423
  apply (induct n)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3424
   apply simp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3425
  apply (simp add: shiftr1_div_2 mult.commute zdiv_zmult2_eq [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3426
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3427
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3428
lemma sshiftr_div_2n: "sint (sshiftr w n) = sint w div 2 ^ n"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3429
  apply transfer
72241
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3430
  apply (rule bit_eqI)
5a6d8675bf4b generalized signed_take_bit
haftmann
parents: 72239
diff changeset
  3431
  apply (simp add: bit_signed_take_bit_iff bit_drop_bit_eq min_def flip: drop_bit_eq_div)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3432
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3433
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3434
lemma bit_bshiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3435
  \<open>bit (bshiftr1 b w) n \<longleftrightarrow> b \<and> n = LENGTH('a) - 1 \<or> bit w (Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3436
  for w :: \<open>'a::len word\<close>
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3437
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3438
  apply (simp add: bit_take_bit_iff flip: bit_Suc)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3439
    apply (subst disjunctive_add)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3440
   apply (auto simp add: bit_take_bit_iff bit_or_iff bit_exp_iff simp flip: bit_Suc)
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3441
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3442
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3443
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3444
subsubsection \<open>shift functions in terms of lists of bools\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3445
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3446
lemma shiftl1_rev: "shiftl1 w = word_reverse (shiftr1 (word_reverse w))"
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3447
  apply (rule bit_word_eqI)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3448
  apply (auto simp add: bit_shiftl1_iff bit_word_reverse_iff bit_shiftr1_iff Suc_diff_Suc)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3449
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3450
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3451
lemma shiftl_rev: "shiftl w n = word_reverse (shiftr (word_reverse w) n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3452
  by (induct n) (auto simp add: shiftl_def shiftr_def shiftl1_rev)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3453
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3454
lemma rev_shiftl: "word_reverse w << n = word_reverse (w >> n)"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3455
  by (simp add: shiftl_rev)
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3456
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3457
lemma shiftr_rev: "w >> n = word_reverse (word_reverse w << n)"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3458
  by (simp add: rev_shiftl)
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3459
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3460
lemma rev_shiftr: "word_reverse w >> n = word_reverse (w << n)"
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  3461
  by (simp add: shiftr_rev)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3462
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3463
lemma shiftl_numeral [simp]:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3464
  \<open>numeral k << numeral l = (push_bit (numeral l) (numeral k) :: 'a::len word)\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3465
  by (fact shiftl_word_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3466
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3467
lemma shiftl_zero_size: "size x \<le> n \<Longrightarrow> x << n = 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
  3468
  for x :: "'a::len word"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3469
  apply transfer
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3470
  apply (simp add: take_bit_push_bit)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3471
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3472
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  3473
\<comment> \<open>note -- the following results use \<open>'a::len word < number_ring\<close>\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3474
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3475
lemma shiftl1_2t: "shiftl1 w = 2 * w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3476
  for w :: "'a::len word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3477
  by (simp add: shiftl1_eq wi_hom_mult [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3478
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3479
lemma shiftl1_p: "shiftl1 w = w + w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3480
  for w :: "'a::len word"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
  3481
  by (simp add: shiftl1_2t)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3482
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3483
lemma shiftl_t2n: "shiftl w n = 2 ^ n * w"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3484
  for w :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3485
  by (induct n) (auto simp: shiftl_def shiftl1_2t)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3486
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3487
lemma shiftr1_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
  3488
  "(shiftr1 (numeral w) :: 'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3489
    word_of_int (bin_rest (take_bit (LENGTH('a)) (numeral w)))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3490
  unfolding shiftr1_eq word_numeral_alt by (simp add: word_ubin.eq_norm)
46962
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  3491
5bdcdb28be83 make more word theorems respect int/bin distinction
huffman
parents: 46656
diff changeset
  3492
lemma sshiftr1_sbintr [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3493
  "(sshiftr1 (numeral w) :: 'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3494
    word_of_int (bin_rest (signed_take_bit (LENGTH('a) - 1) (numeral w)))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3495
  unfolding sshiftr1_eq word_numeral_alt by (simp add: word_sbin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3496
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3497
text \<open>TODO: rules for \<^term>\<open>- (numeral n)\<close>\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3498
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3499
lemma drop_bit_word_numeral [simp]:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3500
  \<open>drop_bit (numeral n) (numeral k) =
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3501
    (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
  3502
  by transfer simp
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3503
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3504
lemma shiftr_numeral [simp]:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3505
  \<open>(numeral k >> numeral n :: 'a::len word) = drop_bit (numeral n) (numeral k)\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3506
  by (fact shiftr_word_eq)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3507
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3508
lemma sshiftr_numeral [simp]:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3509
  \<open>(numeral k >>> numeral n :: 'a::len word) =
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3510
    word_of_int (drop_bit (numeral n) (signed_take_bit (LENGTH('a) - 1) (numeral k)))\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3511
  apply (rule word_eqI)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3512
  apply (cases \<open>LENGTH('a)\<close>)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3513
   apply (simp_all add: word_size bit_drop_bit_eq nth_sshiftr nth_sbintr not_le not_less less_Suc_eq_le ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3514
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3515
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3516
lemma zip_replicate: "n \<ge> length ys \<Longrightarrow> zip (replicate n x) ys = map (\<lambda>y. (x, y)) ys"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3517
  apply (induct ys arbitrary: n)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3518
   apply simp_all
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3519
  apply (case_tac n)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3520
   apply simp_all
57492
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56979
diff changeset
  3521
  done
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56979
diff changeset
  3522
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3523
lemma align_lem_or [rule_format] :
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3524
  "\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3525
    drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow>
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3526
    map2 (|) x y = take m x @ drop m y"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3527
  apply (induct y)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3528
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3529
  apply clarsimp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3530
  apply (case_tac x)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3531
   apply force
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3532
  apply (case_tac m)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3533
   apply auto
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59657
diff changeset
  3534
  apply (drule_tac t="length xs" for xs in sym)
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  3535
  apply (auto simp: zip_replicate o_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3536
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3537
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3538
lemma align_lem_and [rule_format] :
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3539
  "\<forall>x m. length x = n + m \<longrightarrow> length y = n + m \<longrightarrow>
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3540
    drop m x = replicate n False \<longrightarrow> take m y = replicate m False \<longrightarrow>
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  3541
    map2 (\<and>) x y = replicate (n + m) False"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3542
  apply (induct y)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3543
   apply force
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3544
  apply clarsimp
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3545
  apply (case_tac x)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3546
   apply force
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3547
  apply (case_tac m)
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3548
  apply auto
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59657
diff changeset
  3549
  apply (drule_tac t="length xs" for xs in sym)
70193
49a65e3f04c9 consolidated map2 clones
haftmann
parents: 70192
diff changeset
  3550
  apply (auto simp: zip_replicate o_def map_replicate_const)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3551
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3552
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  3553
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3554
subsubsection \<open>Mask\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3555
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3556
lemma minus_1_eq_mask:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3557
  \<open>- 1 = (mask LENGTH('a) :: 'a::len word)\<close>
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  3558
  by (rule bit_eqI) (simp add: bit_exp_iff bit_mask_iff exp_eq_zero_iff)
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3559
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3560
lemma mask_eq_decr_exp:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3561
  \<open>mask n = 2 ^ n - (1 :: 'a::len word)\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3562
  by (fact mask_eq_exp_minus_1)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3563
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3564
lemma mask_Suc_rec:
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3565
  \<open>mask (Suc n) = 2 * mask n + (1 :: 'a::len word)\<close>
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3566
  by (simp add: mask_eq_exp_minus_1)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3567
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3568
context
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3569
begin
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3570
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3571
qualified lemma bit_mask_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3572
  \<open>bit (mask m :: 'a::len word) n \<longleftrightarrow> n < min LENGTH('a) m\<close>
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3573
  by (simp add: bit_mask_iff exp_eq_zero_iff not_le)
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3574
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3575
end
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3576
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3577
lemma nth_mask [simp]:
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
  3578
  \<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close>
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  3579
  by (auto simp add: test_bit_word_eq word_size Word.bit_mask_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3580
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3581
lemma mask_bin: "mask n = word_of_int (take_bit n (- 1))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3582
  by (auto simp add: nth_bintr word_size intro: word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3583
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3584
lemma and_mask_bintr: "w AND mask n = word_of_int (take_bit n (uint w))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3585
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3586
  apply (simp add: nth_bintr word_size word_ops_nth_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3587
  apply (auto simp add: test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3588
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3589
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3590
lemma and_mask_wi: "word_of_int i AND mask n = word_of_int (take_bit n i)"
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3591
  by (auto simp add: nth_bintr word_size word_ops_nth_size word_eq_iff)
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  3592
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3593
lemma and_mask_wi':
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3594
  "word_of_int i AND mask n = (word_of_int (take_bit (min LENGTH('a) n) i) :: 'a::len word)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  3595
  by (auto simp add: nth_bintr word_size word_ops_nth_size word_eq_iff)
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  3596
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3597
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
  3598
  unfolding word_numeral_alt by (rule and_mask_wi)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3599
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3600
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
  3601
  by (simp only: and_mask_bintr take_bit_eq_mod)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3602
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3603
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
  3604
  \<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
  3605
  by transfer simp
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3606
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3607
lemma and_mask_lt_2p: "uint (w AND mask n) < 2 ^ n"
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3608
  apply (simp add: uint_and uint_mask_eq)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3609
  apply (rule AND_upper2'')
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3610
  apply simp
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3611
  apply (auto simp add: mask_eq_exp_minus_1 min_def power_add algebra_simps dest!: le_Suc_ex)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3612
  apply (metis add_minus_cancel le_add2 one_le_numeral power_add power_increasing uminus_add_conv_diff zle_diff1_eq)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  3613
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3614
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3615
lemma eq_mod_iff: "0 < n \<Longrightarrow> b = b mod n \<longleftrightarrow> 0 \<le> b \<and> b < n"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3616
  for b n :: int
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3617
  by auto (metis pos_mod_conj)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3618
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  3619
lemma mask_eq_iff: "w AND mask n = w \<longleftrightarrow> uint w < 2 ^ n"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3620
  apply (simp add: and_mask_bintr)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3621
  apply (simp add: word_ubin.inverse_norm)
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3622
  apply (simp add: eq_mod_iff take_bit_eq_mod min_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3623
  apply (fast intro!: lt2p_lem)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3624
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3625
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3626
lemma and_mask_dvd: "2 ^ n dvd uint w \<longleftrightarrow> w AND mask n = 0"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3627
  apply (simp add: dvd_eq_mod_eq_0 and_mask_mod_2p)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3628
  apply (simp add: word_uint.norm_eq_iff [symmetric] word_of_int_homs del: word_of_int_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3629
  apply (subst word_uint.norm_Rep [symmetric])
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3630
  apply (simp only: bintrunc_bintrunc_min take_bit_eq_mod [symmetric] min_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3631
  apply auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3632
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3633
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3634
lemma and_mask_dvd_nat: "2 ^ n dvd unat w \<longleftrightarrow> w AND mask n = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3635
  apply (simp flip: and_mask_dvd)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3636
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3637
  using dvd_nat_abs_iff [of _ \<open>take_bit LENGTH('a) k\<close> for k]
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3638
  apply simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3639
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3640
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3641
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
  3642
  for w :: "'a::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  3643
  apply (unfold word_size word_less_alt word_numeral_alt)
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
  3644
  apply (auto simp add: word_of_int_power_hom word_uint.eq_norm
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3645
      simp del: word_of_int_numeral)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3646
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3647
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3648
lemma less_mask_eq: "x < 2 ^ n \<Longrightarrow> x AND mask n = x"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3649
  for x :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3650
  apply (simp add: and_mask_bintr)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3651
  apply transfer
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3652
  apply (simp add: ac_simps)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3653
  apply (auto simp add: min_def)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  3654
  apply (metis bintrunc_bintrunc_ge mod_pos_pos_trivial mult.commute mult.left_neutral mult_zero_left not_le of_bool_def take_bit_eq_mod take_bit_nonnegative)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3655
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3656
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3657
lemmas mask_eq_iff_w2p = trans [OF mask_eq_iff word_2p_lem [symmetric]]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3658
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3659
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
  3660
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3661
lemma and_mask_less_size: "n < size x \<Longrightarrow> x AND mask n < 2 ^ n"
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  3662
  for x :: \<open>'a::len word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3663
  unfolding word_size by (erule and_mask_less')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3664
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3665
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
  3666
  for c x :: "'a::len word"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3667
  by (auto simp: word_mod_def uint_2p and_mask_mod_2p)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3668
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3669
lemma mask_eqs:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3670
  "(a AND mask n) + b AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3671
  "a + (b AND mask n) AND mask n = a + b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3672
  "(a AND mask n) - b AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3673
  "a - (b AND mask n) AND mask n = a - b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3674
  "a * (b AND mask n) AND mask n = a * b AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3675
  "(b AND mask n) * a AND mask n = b * a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3676
  "(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
  3677
  "(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
  3678
  "(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
  3679
  "- (a AND mask n) AND mask n = - a AND mask n"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3680
  "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
  3681
  "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
  3682
  using word_of_int_Ex [where x=a] word_of_int_Ex [where x=b]
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3683
  by (auto simp: and_mask_wi' word_of_int_homs word.abs_eq_iff take_bit_eq_mod mod_simps)
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3684
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3685
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
  3686
  for x :: \<open>'a::len word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3687
  using word_of_int_Ex [where x=x]
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3688
  by (auto simp: and_mask_wi' word_of_int_power_hom word.abs_eq_iff take_bit_eq_mod mod_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3689
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3690
lemma mask_full [simp]: "mask LENGTH('a) = (- 1 :: 'a::len word)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3691
  by transfer (simp add: take_bit_minus_one_eq_mask)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3692
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3693
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3694
subsubsection \<open>Slices\<close>
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3695
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3696
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
  3697
  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
  3698
    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
  3699
    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
  3700
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3701
lemma bit_slice1_iff:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3702
  \<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
  3703
    \<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
  3704
  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
  3705
  by (auto simp add: slice1_def bit_ucast_iff bit_drop_bit_eq bit_push_bit_iff exp_eq_zero_iff not_less not_le ac_simps
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3706
    dest: bit_imp_le_length)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3707
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3708
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
  3709
  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
  3710
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3711
lemma bit_slice_iff:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3712
  \<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
  3713
  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
  3714
  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
  3715
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3716
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
  3717
  unfolding slice1_def by simp
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3718
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3719
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
  3720
  unfolding slice_def by auto
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3721
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3722
lemma slice_shiftr: "slice n w = ucast (w >> n)"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3723
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3724
  apply (cases \<open>n \<le> LENGTH('b)\<close>)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3725
   apply (auto simp add: bit_slice_iff bit_ucast_iff bit_shiftr_word_iff ac_simps
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3726
    dest: bit_imp_le_length)
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3727
  done
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3728
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3729
lemma nth_slice: "(slice n w :: 'a::len word) !! m = (w !! (m + n) \<and> m < LENGTH('a))"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3730
  by (simp add: slice_shiftr nth_ucast nth_shiftr)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3731
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3732
lemma ucast_slice1: "ucast w = slice1 (size w) w"
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3733
  apply (simp add: slice1_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3734
  apply transfer
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3735
  apply simp
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3736
  done
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3737
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3738
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
  3739
  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
  3740
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3741
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
  3742
  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
  3743
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3744
lemma rev_slice1:
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3745
  \<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
  3746
  if \<open>n + k = LENGTH('a) + LENGTH('b)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3747
proof (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3748
  fix m
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3749
  assume *: \<open>m < LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3750
  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
  3751
    by simp
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3752
  show \<open>bit (slice1 n (word_reverse w :: 'b word) :: 'a word) m \<longleftrightarrow> bit (word_reverse (slice1 k w :: 'a word)) m\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3753
    apply (simp add: bit_slice1_iff bit_word_reverse_iff)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3754
    using * **
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3755
    apply (cases \<open>n \<le> LENGTH('a)\<close>; cases \<open>k \<le> LENGTH('a)\<close>)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3756
       apply auto
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3757
    done
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3758
qed
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3759
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3760
lemma rev_slice:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3761
  "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
  3762
    slice n (word_reverse (w::'b word)) = word_reverse (slice k w :: 'a word)"
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3763
  apply (unfold slice_def word_size)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3764
  apply (rule rev_slice1)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3765
  apply arith
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3766
  done
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3767
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3768
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3769
subsubsection \<open>Revcast\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3770
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3771
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
  3772
  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
  3773
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3774
lemma bit_revcast_iff:
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3775
  \<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
  3776
    \<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
  3777
  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
  3778
  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
  3779
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3780
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
  3781
  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
  3782
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3783
lemma revcast_rev_ucast [OF refl refl refl]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3784
  "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
  3785
    rc = word_reverse uc"
72027
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3786
  apply auto
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3787
  apply (rule bit_word_eqI)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3788
  apply (cases \<open>LENGTH('a) \<le> LENGTH('b)\<close>)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3789
   apply (simp_all add: bit_revcast_iff bit_word_reverse_iff bit_ucast_iff not_le
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3790
     bit_imp_le_length)
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3791
  using bit_imp_le_length apply fastforce
759532ef0885 prefer canonically oriented lists of bits and more direct characterizations in definitions
haftmann
parents: 72010
diff changeset
  3792
  using bit_imp_le_length apply fastforce
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3793
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3794
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3795
lemma revcast_ucast: "revcast w = word_reverse (ucast (word_reverse w))"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3796
  using revcast_rev_ucast [of "word_reverse w"] by simp
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3797
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3798
lemma ucast_revcast: "ucast w = word_reverse (revcast (word_reverse w))"
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3799
  by (fact revcast_rev_ucast [THEN word_rev_gal'])
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3800
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3801
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
  3802
  by (fact revcast_ucast [THEN word_rev_gal'])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3803
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3804
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3805
text "linking revcast and cast via shift"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3806
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3807
lemmas wsst_TYs = source_size target_size word_size
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3808
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3809
lemma revcast_down_uu [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3810
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3811
  for w :: "'a::len word"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3812
  apply (simp add: source_size_def target_size_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3813
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3814
  apply (simp add: bit_revcast_iff bit_ucast_iff bit_shiftr_word_iff ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3815
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3816
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3817
lemma revcast_down_us [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3818
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >>> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3819
  for w :: "'a::len word"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3820
  apply (simp add: source_size_def target_size_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3821
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3822
  apply (simp add: bit_revcast_iff bit_ucast_iff bit_sshiftr_word_iff ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3823
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3824
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3825
lemma revcast_down_su [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3826
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3827
  for w :: "'a::len word"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3828
  apply (simp add: source_size_def target_size_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3829
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3830
  apply (simp add: bit_revcast_iff bit_word_scast_iff bit_shiftr_word_iff ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3831
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3832
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3833
lemma revcast_down_ss [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3834
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >>> n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3835
  for w :: "'a::len word"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3836
  apply (simp add: source_size_def target_size_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3837
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3838
  apply (simp add: bit_revcast_iff bit_word_scast_iff bit_sshiftr_word_iff ac_simps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3839
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3840
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3841
lemma cast_down_rev [OF refl]:
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3842
  "uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> uc w = revcast (w << n)"
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  3843
  for w :: "'a::len word"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3844
  apply (simp add: source_size_def target_size_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3845
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3846
  apply (simp add: bit_revcast_iff bit_word_ucast_iff bit_shiftl_word_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3847
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3848
45811
f506015ca2dc replace many uses of 'lemmas' with 'lemma';
huffman
parents: 45810
diff changeset
  3849
lemma revcast_up [OF refl]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3850
  "rc = revcast \<Longrightarrow> source_size rc + n = target_size rc \<Longrightarrow>
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3851
    rc w = (ucast w :: 'a::len word) << n"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3852
  apply (simp add: source_size_def target_size_def)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3853
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3854
  apply (simp add: bit_revcast_iff bit_word_ucast_iff bit_shiftl_word_iff)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3855
  apply auto
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3856
  apply (metis add.commute add_diff_cancel_right)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3857
  apply (metis diff_add_inverse2 diff_diff_add)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3858
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3859
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3860
lemmas rc1 = revcast_up [THEN
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3861
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3862
lemmas rc2 = revcast_down_uu [THEN
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3863
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3864
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3865
lemmas ucast_up =
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3866
 rc1 [simplified rev_shiftr [symmetric] revcast_ucast [symmetric]]
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3867
lemmas ucast_down =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3868
  rc2 [simplified rev_shiftr revcast_ucast [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3869
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3870
lemmas sym_notr =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3871
  not_iff [THEN iffD2, THEN not_sym, THEN not_iff [THEN iffD1]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3872
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3873
\<comment> \<open>problem posed by TPHOLs referee:
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3874
      criterion for overflow of addition of signed integers\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3875
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3876
lemma sofl_test:
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3877
  \<open>sint x + sint y = sint (x + y) \<longleftrightarrow>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3878
    (x + y XOR x) AND (x + y XOR y) >> (size x - 1) = 0\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3879
  for x y :: \<open>'a::len word\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3880
proof -
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3881
  obtain n where n: \<open>LENGTH('a) = Suc n\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3882
    by (cases \<open>LENGTH('a)\<close>) simp_all
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3883
  have *: \<open>sint x + sint y + 2 ^ Suc n > signed_take_bit n (sint x + sint y) \<Longrightarrow> sint x + sint y \<ge> - (2 ^ n)\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3884
    \<open>signed_take_bit n (sint x + sint y) > sint x + sint y - 2 ^ Suc n \<Longrightarrow> 2 ^ n > sint x + sint y\<close>
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
  3885
    using signed_take_bit_int_greater_eq [of \<open>sint x + sint y\<close> n] signed_take_bit_int_less_eq [of n \<open>sint x + sint y\<close>]
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3886
    by (auto intro: ccontr)
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3887
  have \<open>sint x + sint y = sint (x + y) \<longleftrightarrow>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3888
    (sint (x + y) < 0 \<longleftrightarrow> sint x < 0) \<or>
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3889
    (sint (x + y) < 0 \<longleftrightarrow> sint y < 0)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3890
    using sint_range' [of x] sint_range' [of y]
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3891
    apply (auto simp add: not_less)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3892
       apply (unfold sint_word_ariths word_sbin.set_iff_norm [symmetric] sints_num)
a851ce626b78 signed_take_bit
haftmann
parents: 72009
diff changeset
  3893
       apply (auto simp add: signed_take_bit_eq_take_bit_minus take_bit_Suc_from_most n not_less intro!: *)
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3894
    done
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3895
  then show ?thesis
72227
0f3d24dc197f more on conversions
haftmann
parents: 72130
diff changeset
  3896
    apply (simp only: One_nat_def word_size shiftr_word_eq drop_bit_eq_zero_iff_not_bit_last bit_and_iff bit_xor_iff)
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3897
    apply (simp add: bit_last_iff)
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3898
    done
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
  3899
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3900
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3901
lemma shiftr_zero_size: "size x \<le> n \<Longrightarrow> x >> n = 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
  3902
  for x :: "'a :: len word"
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3903
  by (rule word_eqI) (auto simp add: nth_shiftr dest: test_bit_size)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  3904
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3905
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3906
subsection \<open>Split and cat\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3907
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  3908
lemmas word_split_bin' = word_split_def
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  3909
lemmas word_cat_bin' = word_cat_eq
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3910
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3911
lemma word_rsplit_no:
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
  3912
  "(word_rsplit (numeral bin :: 'b::len word) :: 'a word list) =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  3913
    map word_of_int (bin_rsplit (LENGTH('a::len))
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3914
      (LENGTH('b), take_bit (LENGTH('b)) (numeral bin)))"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3915
  by (simp add: word_rsplit_def word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3916
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3917
lemmas word_rsplit_no_cl [simp] = word_rsplit_no
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3918
  [unfolded bin_rsplitl_def bin_rsplit_l [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3919
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3920
lemma test_bit_cat [OF refl]:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3921
  "wc = word_cat a b \<Longrightarrow> wc !! n = (n < size wc \<and>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3922
    (if n < size b then b !! n else a !! (n - size b)))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3923
  apply (simp add: word_size not_less; transfer)
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3924
       apply (auto simp add: bit_concat_bit_iff bit_take_bit_iff)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3925
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3926
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3927
lemma split_uint_lem: "bin_split n (uint w) = (a, b) \<Longrightarrow>
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  3928
    a = take_bit (LENGTH('a) - n) a \<and> b = take_bit (LENGTH('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
  3929
  for w :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3930
  apply (frule word_ubin.norm_Rep [THEN ssubst])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3931
  apply (drule bin_split_trunc1)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3932
  apply (drule sym [THEN trans])
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3933
   apply assumption
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3934
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3935
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3936
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  3937
\<comment> \<open>keep quantifiers for use in simplification\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3938
lemma test_bit_split':
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3939
  "word_split c = (a, b) \<longrightarrow>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3940
    (\<forall>n m.
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3941
      b !! n = (n < size b \<and> c !! n) \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3942
      a !! m = (m < size a \<and> c !! (m + size b)))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3943
  apply (unfold word_split_bin' test_bit_bin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3944
  apply (clarify)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3945
  apply (clarsimp simp: word_ubin.eq_norm nth_bintr word_size split: prod.splits)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71948
diff changeset
  3946
  apply (auto simp add: bit_take_bit_iff bit_drop_bit_eq ac_simps bin_nth_uint_imp)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3947
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3948
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3949
lemma test_bit_split:
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3950
  "word_split c = (a, b) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3951
    (\<forall>n::nat. b !! n \<longleftrightarrow> n < size b \<and> c !! n) \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3952
    (\<forall>m::nat. a !! m \<longleftrightarrow> m < size a \<and> c !! (m + size b))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3953
  by (simp add: test_bit_split')
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3954
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3955
lemma test_bit_split_eq:
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3956
  "word_split c = (a, b) \<longleftrightarrow>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3957
    ((\<forall>n::nat. b !! n = (n < size b \<and> c !! n)) \<and>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3958
     (\<forall>m::nat. a !! m = (m < size a \<and> c !! (m + size b))))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3959
  apply (rule_tac iffI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3960
   apply (rule_tac conjI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3961
    apply (erule test_bit_split [THEN conjunct1])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3962
   apply (erule test_bit_split [THEN conjunct2])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3963
  apply (case_tac "word_split c")
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3964
  apply (frule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3965
  apply (erule trans)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3966
  apply (fastforce intro!: word_eqI simp add: word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3967
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3968
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  3969
\<comment> \<open>this odd result is analogous to \<open>ucast_id\<close>,
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3970
      result to the length given by the result type\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3971
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3972
lemma word_cat_id: "word_cat a b = b"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3973
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3974
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
  3975
\<comment> \<open>limited hom result\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3976
lemma word_cat_hom:
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
  3977
  "LENGTH('a::len) \<le> LENGTH('b::len) + LENGTH('c::len) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3978
    (word_cat (word_of_int w :: 'b word) (b :: 'c word) :: 'a word) =
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3979
    word_of_int (bin_cat w (size b) (uint b))"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3980
  apply transfer
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  3981
  using bintr_cat by auto
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3982
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3983
lemma word_cat_split_alt: "size w \<le> size u + size v \<Longrightarrow> word_split w = (u, v) \<Longrightarrow> word_cat u v = w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3984
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3985
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3986
  apply (clarsimp simp add : test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3987
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3988
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3989
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3990
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  3991
lemmas word_cat_split_size = sym [THEN [2] word_cat_split_alt [symmetric]]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3992
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3993
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  3994
subsubsection \<open>Split and slice\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3995
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  3996
lemma split_slices: "word_split w = (u, v) \<Longrightarrow> u = slice (size v) w \<and> v = slice 0 w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3997
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3998
  apply (rule conjI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  3999
   apply (rule word_eqI, clarsimp simp: nth_slice word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4000
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4001
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4002
lemma slice_cat1 [OF refl]:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4003
  "wc = word_cat a b \<Longrightarrow> size wc >= size a + size b \<Longrightarrow> slice (size b) wc = a"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4004
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4005
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4006
  apply (simp add: nth_slice test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4007
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4008
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4009
lemmas slice_cat2 = trans [OF slice_id word_cat_id]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4010
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4011
lemma cat_slices:
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4012
  "a = slice n c \<Longrightarrow> b = slice 0 c \<Longrightarrow> n = size b \<Longrightarrow>
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4013
    size a + size b >= size c \<Longrightarrow> word_cat a b = c"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4014
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4015
  apply (rule word_eqI)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4016
  apply (simp add: nth_slice test_bit_cat word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4017
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4018
  apply arith
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4019
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4020
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4021
lemma word_split_cat_alt:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4022
  "w = word_cat u v \<Longrightarrow> size u + size v \<le> size w \<Longrightarrow> word_split w = (u, v)"
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 59657
diff changeset
  4023
  apply (case_tac "word_split w")
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4024
  apply (rule trans, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4025
  apply (drule test_bit_split)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4026
  apply safe
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4027
   apply (rule word_eqI, clarsimp simp: test_bit_cat word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4028
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4029
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4030
text \<open>
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4031
  This odd result arises from the fact that the statement of the
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4032
  result implies that the decoded words are of the same type,
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4033
  and therefore of the same length, as the original word.\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4034
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4035
lemma word_rsplit_same: "word_rsplit w = [w]"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4036
  by (simp add: word_rsplit_def bin_rsplit_all)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4037
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4038
lemma word_rsplit_empty_iff_size: "word_rsplit w = [] \<longleftrightarrow> size w = 0"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4039
  by (simp add: word_rsplit_def bin_rsplit_def word_size bin_rsplit_aux_simp_alt Let_def
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4040
      split: prod.split)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4041
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4042
lemma test_bit_rsplit:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4043
  "sw = word_rsplit w \<Longrightarrow> m < size (hd sw) \<Longrightarrow>
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4044
    k < length sw \<Longrightarrow> (rev sw ! k) !! m = w !! (k * size (hd sw) + m)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4045
  for sw :: "'a::len word list"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4046
  apply (unfold word_rsplit_def word_test_bit_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4047
  apply (rule trans)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4048
   apply (rule_tac f = "\<lambda>x. bin_nth x m" in arg_cong)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4049
   apply (rule nth_map [symmetric])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4050
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4051
  apply (rule bin_nth_rsplit)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4052
     apply simp_all
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4053
  apply (simp add : word_size rev_map)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4054
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4055
   defer
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4056
   apply (rule map_ident [THEN fun_cong])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4057
  apply (rule refl [THEN map_cong])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4058
  apply (simp add : word_ubin.eq_norm)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4059
  apply (erule bin_rsplit_size_sign [OF len_gt_0 refl])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4060
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4061
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4062
lemma horner_sum_uint_exp_Cons_eq:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4063
  \<open>horner_sum uint (2 ^ LENGTH('a)) (w # ws) =
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4064
    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
  4065
  for ws :: \<open>'a::len word list\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4066
  by (simp add: concat_bit_eq push_bit_eq_mult)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4067
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4068
lemma bit_horner_sum_uint_exp_iff:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4069
  \<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
  4070
    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
  4071
  for ws :: \<open>'a::len word list\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4072
proof (induction ws arbitrary: n)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4073
  case Nil
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4074
  then show ?case
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4075
    by simp
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4076
next
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4077
  case (Cons w ws)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4078
  then show ?case
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4079
    by (cases \<open>n \<ge> LENGTH('a)\<close>)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4080
      (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
  4081
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4082
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4083
lemma test_bit_rcat:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4084
  "sw = size (hd wl) \<Longrightarrow> rc = word_rcat wl \<Longrightarrow> rc !! n =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4085
    (n < size rc \<and> n div sw < size wl \<and> (rev wl) ! (n div sw) !! (n mod sw))"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4086
  for wl :: "'a::len word list"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4087
  by (simp add: word_size word_rcat_def bin_rcat_def foldl_map rev_map bit_horner_sum_uint_exp_iff)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4088
    (simp add: test_bit_eq_bit)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4089
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4090
lemmas test_bit_cong = arg_cong [where f = "test_bit", THEN fun_cong]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4091
71996
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4092
lemma test_bit_rsplit_alt:
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4093
  \<open>(word_rsplit w  :: 'b::len word list) ! i !! m \<longleftrightarrow>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4094
    w !! ((length (word_rsplit w :: 'b::len word list) - Suc i) * size (hd (word_rsplit w :: 'b::len word list)) + m)\<close>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4095
  if \<open>i < length (word_rsplit w :: 'b::len word list)\<close> \<open>m < size (hd (word_rsplit w :: 'b::len word list))\<close> \<open>0 < length (word_rsplit w :: 'b::len word list)\<close>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4096
  for w :: \<open>'a::len word\<close>
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4097
  apply (rule trans)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4098
   apply (rule test_bit_cong)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4099
   apply (rule rev_nth [of _ \<open>rev (word_rsplit w)\<close>, simplified rev_rev_ident])
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4100
  apply simp
71996
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4101
   apply (rule that(1))
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4102
  apply simp
71996
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4103
  apply (rule test_bit_rsplit)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4104
    apply (rule refl)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4105
  apply (rule asm_rl)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4106
   apply (rule that(2))
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4107
  apply (rule diff_Suc_less)
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4108
  apply (rule that(3))
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4109
  done
c7ac6d4f3914 prefer explicit proof
haftmann
parents: 71991
diff changeset
  4110
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4111
lemma word_rsplit_len_indep [OF refl refl refl refl]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4112
  "[u,v] = p \<Longrightarrow> [su,sv] = q \<Longrightarrow> word_rsplit u = su \<Longrightarrow>
40827
abbc05c20e24 code preprocessor setup for numerals on word type;
haftmann
parents: 39910
diff changeset
  4113
    word_rsplit v = sv \<Longrightarrow> length su = length sv"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4114
  by (auto simp: word_rsplit_def bin_rsplit_len_indep)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4115
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4116
lemma length_word_rsplit_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4117
  "n = LENGTH('a::len) \<Longrightarrow>
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4118
    length (word_rsplit w :: 'a word list) \<le> m \<longleftrightarrow> size w \<le> m * n"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4119
  by (auto simp: word_rsplit_def word_size bin_rsplit_len_le)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4120
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4121
lemmas length_word_rsplit_lt_size =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4122
  length_word_rsplit_size [unfolded Not_eq_iff linorder_not_less [symmetric]]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4123
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4124
lemma length_word_rsplit_exp_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4125
  "n = LENGTH('a::len) \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4126
    length (word_rsplit w :: 'a word list) = (size w + n - 1) div n"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4127
  by (auto simp: word_rsplit_def word_size bin_rsplit_len)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4128
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4129
lemma length_word_rsplit_even_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4130
  "n = LENGTH('a::len) \<Longrightarrow> size w = m * n \<Longrightarrow>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4131
    length (word_rsplit w :: 'a word list) = m"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4132
  by (cases \<open>LENGTH('a)\<close>) (simp_all add: length_word_rsplit_exp_size div_nat_eqI)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4133
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4134
lemmas length_word_rsplit_exp_size' = refl [THEN length_word_rsplit_exp_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4135
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  4136
\<comment> \<open>alternative proof of \<open>word_rcat_rsplit\<close>\<close>
66808
1907167b6038 elementary definition of division on natural numbers
haftmann
parents: 66453
diff changeset
  4137
lemmas tdle = times_div_less_eq_dividend
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4138
lemmas dtle = xtrans(4) [OF tdle mult.commute]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4139
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4140
lemma word_rcat_rsplit: "word_rcat (word_rsplit w) = w"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4141
  apply (rule word_eqI)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4142
  apply (clarsimp simp: test_bit_rcat word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4143
  apply (subst refl [THEN test_bit_rsplit])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4144
    apply (simp_all add: word_size
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4145
      refl [THEN length_word_rsplit_size [simplified not_less [symmetric], simplified]])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4146
   apply safe
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4147
   apply (erule xtrans(7), rule dtle)+
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4148
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4149
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4150
lemma size_word_rsplit_rcat_size:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4151
  "word_rcat ws = frcw \<Longrightarrow> size frcw = length ws * LENGTH('a)
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4152
    \<Longrightarrow> length (word_rsplit frcw::'a word list) = length ws"
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
  4153
  for ws :: "'a::len word list" and frcw :: "'b::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4154
  by (cases \<open>LENGTH('a)\<close>) (simp_all add: word_size length_word_rsplit_exp_size' div_nat_eqI)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4155
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4156
lemma msrevs:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4157
  "0 < n \<Longrightarrow> (k * n + m) div n = m div n + k"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4158
  "(k * n + m) mod n = m mod n"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4159
  for n :: nat
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57492
diff changeset
  4160
  by (auto simp: add.commute)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4161
45816
6a04efd99f25 replace more uses of 'lemmas' with explicit 'lemma';
huffman
parents: 45811
diff changeset
  4162
lemma word_rsplit_rcat_size [OF refl]:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4163
  "word_rcat ws = frcw \<Longrightarrow>
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4164
    size frcw = length ws * LENGTH('a) \<Longrightarrow> word_rsplit frcw = ws"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4165
  for ws :: "'a::len word list"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4166
  apply (frule size_word_rsplit_rcat_size, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4167
  apply (clarsimp simp add : word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4168
  apply (rule nth_equalityI, assumption)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4169
  apply clarsimp
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46022
diff changeset
  4170
  apply (rule word_eqI [rule_format])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4171
  apply (rule trans)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4172
   apply (rule test_bit_rsplit_alt)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4173
     apply (clarsimp simp: word_size)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4174
  apply (rule trans)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4175
   apply (rule test_bit_rcat [OF refl refl])
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  4176
  apply (simp add: word_size)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4177
  apply (subst rev_nth)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4178
   apply arith
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4179
  apply (simp add: le0 [THEN [2] xtrans(7), THEN diff_Suc_less])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4180
  apply safe
41550
efa734d9b221 eliminated global prems;
wenzelm
parents: 41413
diff changeset
  4181
  apply (simp add: diff_mult_distrib)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4182
   apply (cases "size ws")
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4183
    apply simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4184
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4185
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4186
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4187
subsection \<open>Rotation\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4188
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4189
lemma word_rotr_word_rotr_eq:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4190
  \<open>word_rotr m (word_rotr n w) = word_rotr (m + n) w\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4191
  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
  4192
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4193
lemma word_rot_rl [simp]:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4194
  \<open>word_rotl k (word_rotr k v) = v\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4195
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4196
  apply (simp add: word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4197
  apply (auto dest: bit_imp_le_length)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4198
   apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_if mod_mult_self2_is_0)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  4199
  apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_less mod_mult_self2_is_0)
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4200
  done
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4201
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4202
lemma word_rot_lr [simp]:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4203
  \<open>word_rotr k (word_rotl k v) = v\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4204
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4205
  apply (simp add: word_rotl_eq_word_rotr word_rotr_word_rotr_eq bit_word_rotr_iff algebra_simps)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4206
  apply (auto dest: bit_imp_le_length)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4207
   apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_if mod_mult_self2_is_0)
72239
12e94c2ff6c5 generalized
haftmann
parents: 72227
diff changeset
  4208
  apply (metis (no_types, lifting) add.right_neutral add_diff_cancel_right' div_mult_mod_eq mod_add_right_eq mod_add_self2 mod_less mod_mult_self2_is_0)
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4209
  done
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_rot_gal:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4212
  \<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
  4213
  by auto
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4214
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4215
lemma word_rot_gal':
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4216
  \<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
  4217
  by auto
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4218
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4219
lemma word_rotr_rev:
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4220
  \<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
  4221
proof (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4222
  fix m
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4223
  assume \<open>m < LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4224
  moreover have \<open>1 +
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4225
    ((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
  4226
     ((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
  4227
    int LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4228
    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
  4229
         int LENGTH('a) = 0\<close>)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4230
    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
  4231
    apply simp_all
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4232
     apply (auto simp add: algebra_simps)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4233
     apply (simp add: minus_equation_iff [of \<open>int m\<close>])
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4234
     apply (drule sym [of _ \<open>int m\<close>])
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4235
    apply simp
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4236
    apply (metis add.commute add_minus_cancel diff_minus_eq_add len_gt_0 less_imp_of_nat_less less_nat_zero_code mod_mult_self3 of_nat_gt_0 zmod_minus1)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4237
    apply (metis (no_types, hide_lams) Abs_fnat_hom_add less_not_refl mod_Suc of_nat_Suc of_nat_gt_0 of_nat_mod)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4238
    done
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4239
  then have \<open>int ((m + n) mod LENGTH('a)) =
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4240
    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
  4241
    using \<open>m < LENGTH('a)\<close>
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4242
    by (simp only: of_nat_mod mod_simps)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4243
      (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
  4244
  then have \<open>(m + n) mod LENGTH('a) =
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4245
    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
  4246
    by simp
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4247
  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
  4248
    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
  4249
qed
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4250
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4251
lemma word_roti_0 [simp]: "word_roti 0 w = w"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4252
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4253
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4254
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
  4255
  by (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4256
    (simp add: bit_word_roti_iff nat_less_iff mod_simps ac_simps)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4257
67118
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4258
lemma word_roti_conv_mod':
ccab07d1196c more simplification rules
haftmann
parents: 66912
diff changeset
  4259
  "word_roti n w = word_roti (n mod int (size w)) w"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4260
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4261
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4262
lemmas word_roti_conv_mod = word_roti_conv_mod' [unfolded word_size]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4263
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4264
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4265
subsubsection \<open>"Word rotation commutes with bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4266
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  4267
\<comment> \<open>using locale to not pollute lemma namespace\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4268
locale word_rotate
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4269
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4270
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4271
lemma word_rot_logs:
71149
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  4272
  "word_rotl n (NOT v) = NOT (word_rotl n v)"
a7d1fb0c9e16 proper prefix syntax
haftmann
parents: 70901
diff changeset
  4273
  "word_rotr n (NOT v) = NOT (word_rotr n v)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4274
  "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
  4275
  "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
  4276
  "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
  4277
  "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
  4278
  "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
  4279
  "word_rotr n (x XOR y) = word_rotr n x XOR word_rotr n y"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4280
         apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4281
         apply (auto simp add: bit_word_rotl_iff bit_not_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4282
        apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4283
        apply (auto simp add: bit_word_rotr_iff bit_not_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4284
       apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4285
       apply (auto simp add: bit_word_rotl_iff bit_and_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4286
      apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4287
      apply (auto simp add: bit_word_rotr_iff bit_and_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4288
     apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4289
     apply (auto simp add: bit_word_rotl_iff bit_or_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4290
    apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4291
    apply (auto simp add: bit_word_rotr_iff bit_or_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4292
   apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4293
   apply (auto simp add: bit_word_rotl_iff bit_xor_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4294
  apply (rule bit_word_eqI)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4295
  apply (auto simp add: bit_word_rotr_iff bit_xor_iff algebra_simps exp_eq_zero_iff not_le)
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4296
  done
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4297
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4298
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4299
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4300
lemmas word_rot_logs = word_rotate.word_rot_logs
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4301
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4302
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
  4303
  by transfer simp_all
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4304
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4305
lemma word_roti_0' [simp] : "word_roti n 0 = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4306
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4307
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4308
declare word_roti_eq_word_rotr_word_rotl [simp]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4309
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4310
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4311
subsection \<open>Maximum machine word\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4312
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4313
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
  4314
  fixes x :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4315
  obtains n where "x = word_of_int n" and "0 \<le> n" and "n < 2^LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4316
  by (cases x rule: word_uint.Abs_cases) (simp add: uints_num)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4317
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4318
lemma word_nat_cases [cases type: word]:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4319
  fixes x :: "'a::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4320
  obtains n where "x = of_nat n" and "n < 2^LENGTH('a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4321
  by (cases x rule: word_unat.Abs_cases) (simp add: unats_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4322
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4323
lemma max_word_max [intro!]: "n \<le> max_word"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  4324
  by (fact word_order.extremum)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4325
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
  4326
lemma word_of_int_2p_len: "word_of_int (2 ^ LENGTH('a)) = (0::'a::len word)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4327
  by (subst word_uint.Abs_norm [symmetric]) simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4328
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4329
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
  4330
  by (fact word_exp_length_eq_0)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4331
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4332
lemma max_word_wrap: "x + 1 = 0 \<Longrightarrow> x = max_word"
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4333
  by (simp add: eq_neg_iff_add_eq_0)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4334
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
lemma max_test_bit: "(max_word::'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)"
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4336
  by (fact nth_minus1)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4337
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4338
lemma word_and_max: "x AND max_word = x"
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4339
  by (fact word_log_esimps)
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4340
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4341
lemma word_or_max: "x OR max_word = max_word"
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  4342
  by (fact word_log_esimps)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4343
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4344
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
  4345
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4346
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4347
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4348
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
  4349
  for x y z :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4350
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4351
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4352
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
  4353
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4354
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4355
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4356
lemma word_or_not [simp]: "x OR NOT x = max_word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4357
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4358
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4359
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
  4360
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4361
  by (rule word_eqI) (auto simp add: word_ops_nth_size word_size)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4362
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4363
lemma shiftr_x_0 [iff]: "x >> 0 = 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
  4364
  for x :: "'a::len word"
72088
a36db1c8238e separation of reversed bit lists from other material
haftmann
parents: 72083
diff changeset
  4365
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4366
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4367
lemma shiftl_x_0 [simp]: "x << 0 = x"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4368
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4369
  by (simp add: shiftl_t2n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4370
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4371
lemma shiftl_1 [simp]: "(1::'a::len word) << n = 2^n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4372
  by (simp add: shiftl_t2n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4373
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4374
lemma uint_lt_0 [simp]: "uint x < 0 = False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4375
  by (simp add: linorder_not_less)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4376
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4377
lemma shiftr1_1 [simp]: "shiftr1 (1::'a::len word) = 0"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4378
  by transfer simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4379
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4380
lemma shiftr_1[simp]: "(1::'a::len word) >> n = (if n = 0 then 1 else 0)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4381
  by (induct n) (auto simp: shiftr_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4382
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4383
lemma word_less_1 [simp]: "x < 1 \<longleftrightarrow> x = 0"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4384
  for x :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4385
  by (simp add: word_less_nat_alt unat_0_iff)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4386
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
  4387
lemma map_nth_0 [simp]: "map ((!!) (0::'a::len word)) xs = replicate (length xs) False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4388
  by (induct xs) auto
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4389
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4390
lemma uint_plus_if_size:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4391
  "uint (x + y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4392
    (if uint x + uint y < 2^size x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4393
     then uint x + uint y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4394
     else uint x + uint y - 2^size x)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4395
  by (simp add: word_arith_wis int_word_uint mod_add_if_z word_size)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4396
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4397
lemma unat_plus_if_size:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4398
  "unat (x + y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4399
    (if unat x + unat y < 2^size x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4400
     then unat x + unat y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4401
     else unat x + unat y - 2^size x)"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4402
  for x y :: "'a::len word"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4403
  apply (subst word_arith_nat_defs)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4404
  apply (subst unat_of_nat)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4405
  apply (auto simp add: not_less word_size)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4406
  apply (metis not_le unat_plus_if' unat_word_ariths(1))
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4407
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4408
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4409
lemma word_neq_0_conv: "w \<noteq> 0 \<longleftrightarrow> 0 < w"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4410
  for w :: "'a::len word"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4411
  by (simp add: word_gt_0)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4412
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4413
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
  4414
  for c :: "'a::len word"
55818
d8b2f50705d0 more precise imports;
haftmann
parents: 55817
diff changeset
  4415
  by (fact unat_div)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4416
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4417
lemma uint_sub_if_size:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4418
  "uint (x - y) =
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4419
    (if uint y \<le> uint x
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4420
     then uint x - uint y
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4421
     else uint x - uint y + 2^size x)"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4422
  by (simp add: word_arith_wis int_word_uint mod_sub_if_z word_size)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4423
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4424
lemma unat_sub:
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4425
  \<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
  4426
  if \<open>b \<le> a\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4427
proof -
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4428
  from that have \<open>unat b \<le> unat a\<close>
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4429
    by transfer simp
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4430
  with that show ?thesis
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4431
    apply transfer
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4432
  apply simp
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4433
    apply (subst take_bit_diff [symmetric])
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4434
    apply (subst nat_take_bit_eq)
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4435
    apply (simp add: nat_le_eq_zle)
72261
5193570b739a more lemmas
haftmann
parents: 72244
diff changeset
  4436
    apply (simp add: nat_diff_distrib take_bit_nat_eq_self_iff less_imp_diff_less bintr_lt2p)
72130
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4437
    done
9e5862223442 dedicated symbols for code generation, to pave way for generic conversions from and to word
haftmann
parents: 72128
diff changeset
  4438
qed
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4439
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4440
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
  4441
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
  4442
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4443
lemma word_of_int_minus: "word_of_int (2^LENGTH('a) - i) = (word_of_int (-i)::'a::len word)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4444
proof -
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  4445
  have *: "2^LENGTH('a) - i = -i + 2^LENGTH('a)"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4446
    by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4447
  show ?thesis
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4448
    apply (subst *)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4449
    apply (subst word_uint.Abs_norm [symmetric], subst mod_add_self2)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4450
    apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4451
    done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4452
qed
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4453
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4454
lemmas word_of_int_inj =
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4455
  word_uint.Abs_inject [unfolded uints_num, simplified]
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4456
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4457
lemma word_le_less_eq: "x \<le> y \<longleftrightarrow> x = y \<or> x < y"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4458
  for x y :: "'z::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  4459
  by (auto simp add: order_class.le_less)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4460
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4461
lemma mod_plus_cong:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4462
  fixes b b' :: int
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4463
  assumes 1: "b = b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4464
    and 2: "x mod b' = x' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4465
    and 3: "y mod b' = y' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4466
    and 4: "x' + y' = z'"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4467
  shows "(x + y) mod b = z' mod b'"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4468
proof -
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4469
  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
  4470
    by (simp add: mod_add_eq)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4471
  also have "\<dots> = (x' + y') mod b'"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
  4472
    by (simp add: mod_add_eq)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4473
  finally show ?thesis
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4474
    by (simp add: 4)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4475
qed
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4476
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4477
lemma mod_minus_cong:
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4478
  fixes b b' :: int
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4479
  assumes "b = b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4480
    and "x mod b' = x' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4481
    and "y mod b' = y' mod b'"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4482
    and "x' - y' = z'"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4483
  shows "(x - y) mod b = z' mod b'"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4484
  using assms [symmetric] by (auto intro: mod_diff_cong)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4485
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4486
lemma word_induct_less: "P 0 \<Longrightarrow> (\<And>n. n < m \<Longrightarrow> P n \<Longrightarrow> P (1 + n)) \<Longrightarrow> P m"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4487
  for P :: "'a::len word \<Rightarrow> bool"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4488
  apply (cases m)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4489
  apply atomize
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4490
  apply (erule rev_mp)+
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4491
  apply (rule_tac x=m in spec)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4492
  apply (induct_tac n)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4493
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4494
  apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4495
  apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4496
   apply clarsimp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4497
   apply (erule_tac x=n in allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4498
   apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4499
    apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4500
    apply (clarsimp simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4501
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4502
  apply (erule_tac x="of_nat na" in allE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4503
  apply (erule impE)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4504
   apply (simp add: unat_arith_simps)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4505
   apply (clarsimp simp: unat_of_nat)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4506
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4507
  done
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4508
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4509
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
  4510
  for P :: "'a::len word \<Rightarrow> bool"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4511
  by (erule word_induct_less) simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4512
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4513
lemma word_induct2 [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
  4514
  for P :: "'b::len word \<Rightarrow> bool"
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4515
  apply (rule word_induct)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4516
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4517
  apply (case_tac "1 + n = 0")
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4518
   apply auto
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4519
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4520
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  4521
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  4522
subsection \<open>Recursion combinator for words\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  4523
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
  4524
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
  4525
  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
  4526
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4527
lemma word_rec_0: "word_rec z s 0 = z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4528
  by (simp add: word_rec_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4529
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4530
lemma word_rec_Suc: "1 + n \<noteq> 0 \<Longrightarrow> word_rec z s (1 + n) = s n (word_rec z s n)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4531
  for n :: "'a::len word"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4532
  apply (auto simp add: word_rec_def unat_word_ariths)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71996
diff changeset
  4533
  apply (metis (mono_tags, lifting) old.nat.simps(7) unatSuc word_unat.Rep_inverse word_unat.eq_norm word_unat.td_th)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4534
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4535
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 65336
diff changeset
  4536
lemma word_rec_Pred: "n \<noteq> 0 \<Longrightarrow> word_rec z s n = s (n - 1) (word_rec z s (n - 1))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4537
  apply (rule subst[where t="n" and s="1 + (n - 1)"])
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4538
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4539
  apply (subst word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4540
   apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4541
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4542
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4543
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4544
lemma word_rec_in: "f (word_rec z (\<lambda>_. f) n) = word_rec (f z) (\<lambda>_. f) n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4545
  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4546
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  4547
lemma word_rec_in2: "f n (word_rec z f n) = word_rec (f 0 z) (f \<circ> (+) 1) n"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4548
  by (induct n) (simp_all add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4549
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4550
lemma word_rec_twice:
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  4551
  "m \<le> n \<Longrightarrow> word_rec z f n = word_rec (word_rec z f (n - m)) (f \<circ> (+) (n - m)) m"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4552
  apply (erule rev_mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4553
  apply (rule_tac x=z in spec)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4554
  apply (rule_tac x=f in spec)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4555
  apply (induct n)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4556
   apply (simp add: word_rec_0)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4557
  apply clarsimp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4558
  apply (rule_tac t="1 + n - m" and s="1 + (n - m)" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4559
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4560
  apply (case_tac "1 + (n - m) = 0")
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4561
   apply (simp add: word_rec_0)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4562
   apply (rule_tac f = "word_rec a b" for a b in arg_cong)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4563
   apply (rule_tac t="m" and s="m + (1 + (n - m))" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4564
    apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4565
   apply (simp (no_asm_use))
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4566
  apply (simp add: word_rec_Suc word_rec_in2)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4567
  apply (erule impE)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4568
   apply uint_arith
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
  4569
  apply (drule_tac x="x \<circ> (+) 1" in spec)
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4570
  apply (drule_tac x="x 0 xa" in spec)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4571
  apply simp
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4572
  apply (rule_tac t="\<lambda>a. x (1 + (n - m + a))" and s="\<lambda>a. x (1 + (n - m) + a)" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4573
   apply (clarsimp simp add: fun_eq_iff)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4574
   apply (rule_tac t="(1 + (n - m + xb))" and s="1 + (n - m) + xb" in subst)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4575
    apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4576
   apply (rule refl)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4577
  apply (rule refl)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4578
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4579
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4580
lemma word_rec_id: "word_rec z (\<lambda>_. id) n = z"
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4581
  by (induct n) (auto simp add: word_rec_0 word_rec_Suc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4582
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4583
lemma word_rec_id_eq: "\<forall>m < n. f m = id \<Longrightarrow> word_rec z f n = z"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4584
  apply (erule rev_mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4585
  apply (induct n)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4586
   apply (auto simp add: word_rec_0 word_rec_Suc)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4587
   apply (drule spec, erule mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4588
   apply uint_arith
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4589
  apply (drule_tac x=n in spec, erule impE)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4590
   apply uint_arith
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4591
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4592
  done
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4593
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  4594
lemma word_rec_max:
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 58061
diff changeset
  4595
  "\<forall>m\<ge>n. m \<noteq> - 1 \<longrightarrow> f m = id \<Longrightarrow> word_rec z f (- 1) = word_rec z f n"
65336
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4596
  apply (subst word_rec_twice[where n="-1" and m="-1 - n"])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4597
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4598
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4599
  apply (rule word_rec_id_eq)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4600
  apply clarsimp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4601
  apply (drule spec, rule mp, erule mp)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4602
   apply (rule word_plus_mono_right2[OF _ order_less_imp_le])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4603
    prefer 2
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4604
    apply assumption
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4605
   apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4606
  apply (erule contrapos_pn)
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4607
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4608
  apply (drule arg_cong[where f="\<lambda>x. x - n"])
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4609
  apply simp
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4610
  done
8e5274fc0093 misc tuning and modernization;
wenzelm
parents: 65328
diff changeset
  4611
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4612
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4613
subsection \<open>More\<close>
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4614
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4615
lemma test_bit_1' [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
  4616
  "(1 :: 'a :: len word) !! n \<longleftrightarrow> 0 < LENGTH('a) \<and> n = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  4617
  by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4618
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  4619
lemma shiftl0:
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
  4620
  "x << 0 = (x :: 'a :: len word)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  4621
  by (fact shiftl_x_0)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4622
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4623
lemma mask_1: "mask 1 = 1"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4624
  by transfer (simp add: min_def mask_Suc_exp)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4625
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4626
lemma mask_Suc_0: "mask (Suc 0) = 1"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4627
  using mask_1 by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4628
72082
41393ecb57ac uniform mask operation
haftmann
parents: 72079
diff changeset
  4629
lemma mask_numeral: "mask (numeral n) = 2 * mask (pred_numeral n) + (1 :: 'a::len word)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4630
  by (simp add: mask_Suc_rec numeral_eq_Suc)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4631
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  4632
lemma bin_last_bintrunc: "bin_last (take_bit l n) = (l > 0 \<and> bin_last n)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4633
  by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4634
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4635
lemma word_and_1:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4636
  "n AND 1 = (if n !! 0 then 1 else 0)" for n :: "_ word"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4637
  by (rule bit_word_eqI) (auto simp add: bit_and_iff test_bit_eq_bit bit_1_iff intro: gr0I)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4638
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4639
lemma bintrunc_shiftl:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  4640
  "take_bit n (m << i) = take_bit (n - i) m << i"
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  4641
  for m :: int
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4642
  by (rule bit_eqI) (auto simp add: bit_take_bit_iff)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4643
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4644
lemma uint_shiftl:
72128
3707cf7b370b reduced prominence od theory Bits_Int
haftmann
parents: 72102
diff changeset
  4645
  "uint (n << i) = take_bit (size n) (uint n << i)"
72079
8c355e2dd7db more consequent transferability
haftmann
parents: 72043
diff changeset
  4646
  by transfer (simp add: push_bit_take_bit shiftl_eq_push_bit)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4647
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4648
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  4649
subsection \<open>Misc\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  4650
69605
a96320074298 isabelle update -u path_cartouches;
wenzelm
parents: 69064
diff changeset
  4651
ML_file \<open>Tools/word_lib.ML\<close>
a96320074298 isabelle update -u path_cartouches;
wenzelm
parents: 69064
diff changeset
  4652
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
  4653
41060
4199fdcfa3c0 moved smt_word.ML into the directory of the Word library
boehmes
parents: 40827
diff changeset
  4654
end