src/HOL/Word/Word.thy
author haftmann
Fri, 03 Jul 2020 06:18:27 +0000
changeset 71990 66beb9d92e43
parent 71986 76193dd4aec8
child 71991 8bff286878bf
permissions -rw-r--r--
explicit proofs for bit projections
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
70192
b4bf82ed0ad5 separate type class for bit comprehension
haftmann
parents: 70191
diff changeset
    13
  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
    14
  Misc_Typedef
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    15
  Misc_Arithmetic
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    16
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    17
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    18
subsection \<open>Prelude\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    19
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    20
lemma (in semiring_bit_shifts) bit_push_bit_iff: \<comment> \<open>TODO move\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    21
  \<open>bit (push_bit m a) n \<longleftrightarrow> m \<le> n \<and> 2 ^ n \<noteq> 0 \<and> bit a (n - m)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    22
  by (auto simp add: bit_iff_odd push_bit_eq_mult even_mult_exp_div_exp_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    23
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    24
lemma (in semiring_bit_shifts) push_bit_numeral [simp]: \<comment> \<open>TODO: move\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    25
  \<open>push_bit (numeral l) (numeral k) = push_bit (pred_numeral l) (numeral (Num.Bit0 k))\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    26
  by (simp add: numeral_eq_Suc mult_2_right) (simp add: numeral_Bit0)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    27
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    28
lemma minus_mod_int_eq: \<comment> \<open>TODO move\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    29
  \<open>- k mod l = l - 1 - (k - 1) mod l\<close> if \<open>l \<ge> 0\<close> for k l :: int
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    30
proof (cases \<open>l = 0\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    31
  case True
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    32
  then show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    33
    by simp 
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    34
next
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    35
  case False
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    36
  with that have \<open>l > 0\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    37
    by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    38
  then show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    39
  proof (cases \<open>l dvd k\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    40
    case True
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    41
    then obtain j where \<open>k = l * j\<close> ..
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    42
    moreover have \<open>(l * j mod l - 1) mod l = l - 1\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    43
      using \<open>l > 0\<close> by (simp add: zmod_minus1)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    44
    then have \<open>(l * j - 1) mod l = l - 1\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    45
      by (simp only: mod_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    46
    ultimately show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    47
      by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    48
  next
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    49
    case False
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    50
    moreover have \<open>0 < k mod l\<close> \<open>k mod l < 1 + l\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    51
      using \<open>0 < l\<close> le_imp_0_less pos_mod_conj False apply auto
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    52
      using le_less apply fastforce
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    53
      using pos_mod_bound [of l k] apply linarith 
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    54
      done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    55
    with \<open>l > 0\<close> have \<open>(k mod l - 1) mod l = k mod l - 1\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    56
      by (simp add: zmod_trival_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    57
    ultimately show ?thesis
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    58
      apply (simp only: zmod_zminus1_eq_if)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    59
      apply (simp add: mod_eq_0_iff_dvd algebra_simps mod_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    60
      done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    61
  qed
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    62
qed
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    63
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    64
lemma nth_rotate: \<comment> \<open>TODO move\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    65
  \<open>rotate m xs ! n = xs ! ((m + n) mod length xs)\<close> if \<open>n < length xs\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    66
  using that apply (auto simp add: rotate_drop_take nth_append not_less less_diff_conv ac_simps dest!: le_Suc_ex)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    67
   apply (metis add.commute mod_add_right_eq mod_less)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    68
  apply (metis (no_types, lifting) Nat.diff_diff_right add.commute add_diff_cancel_right' diff_le_self dual_order.strict_trans2 length_greater_0_conv less_nat_zero_code list.size(3) mod_add_right_eq mod_add_self2 mod_le_divisor mod_less)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    69
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    70
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    71
lemma nth_rotate1: \<comment> \<open>TODO move\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    72
  \<open>rotate1 xs ! n = xs ! (Suc n mod length xs)\<close> if \<open>n < length xs\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    73
  using that nth_rotate [of n xs 1] by simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
    74
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
    75
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
    76
subsection \<open>Type definition\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    77
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
    78
quotient_type (overloaded) 'a word = int / \<open>\<lambda>k l. take_bit LENGTH('a) k = take_bit LENGTH('a::len) l\<close>
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    79
  morphisms rep_word word_of_int by (auto intro!: equivpI reflpI sympI transpI)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    80
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
    81
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
    82
  is \<open>take_bit LENGTH('a)\<close> .
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
    83
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    84
lemma uint_nonnegative: "0 \<le> uint w"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    85
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    86
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
    87
lemma uint_bounded: "uint w < 2 ^ LENGTH('a)"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    88
  for w :: "'a::len word"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    89
  by transfer (simp add: take_bit_eq_mod)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    90
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
    91
lemma uint_idem: "uint w mod 2 ^ LENGTH('a) = uint w"
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
    92
  for w :: "'a::len word"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    93
  using uint_nonnegative uint_bounded by (rule mod_pos_pos_trivial)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
    94
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    95
lemma word_uint_eqI: "uint a = uint b \<Longrightarrow> a = b"
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    96
  by transfer simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    97
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    98
lemma word_uint_eq_iff: "a = b \<longleftrightarrow> uint a = uint b"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
    99
  using word_uint_eqI by auto
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   100
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
   101
lemma uint_word_of_int: "uint (word_of_int k :: 'a::len word) = k mod 2 ^ LENGTH('a)"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   102
  by transfer (simp add: take_bit_eq_mod)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   103
  
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   104
lemma word_of_int_uint: "word_of_int (uint w) = w"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   105
  by transfer simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
   106
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
   107
lemma split_word_all: "(\<And>x::'a::len word. PROP P x) \<equiv> (\<And>x. PROP P (word_of_int x))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   108
proof
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   109
  fix x :: "'a word"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   110
  assume "\<And>x. PROP P (word_of_int x)"
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   111
  then have "PROP P (word_of_int (uint x))" .
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   112
  then show "PROP P x" by (simp add: word_of_int_uint)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   113
qed
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   114
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   115
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   116
subsection \<open>Type conversions and casting\<close>
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   117
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   118
definition sint :: "'a::len word \<Rightarrow> int"
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   119
  \<comment> \<open>treats the most-significant-bit as a sign bit\<close>
70175
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
   120
  where sint_uint: "sint w = sbintrunc (LENGTH('a) - 1) (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   121
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
   122
definition unat :: "'a::len word \<Rightarrow> nat"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   123
  where "unat w = nat (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   124
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   125
definition uints :: "nat \<Rightarrow> int set"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
   126
  \<comment> \<open>the sets of integers representing the words\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   127
  where "uints n = range (bintrunc n)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   128
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   129
definition sints :: "nat \<Rightarrow> int set"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   130
  where "sints n = range (sbintrunc (n - 1))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   131
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   132
lemma uints_num: "uints n = {i. 0 \<le> i \<and> i < 2 ^ n}"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   133
  by (simp add: uints_def range_bintrunc)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   134
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   135
lemma sints_num: "sints n = {i. - (2 ^ (n - 1)) \<le> i \<and> i < 2 ^ (n - 1)}"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   136
  by (simp add: sints_def range_sbintrunc)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   137
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   138
definition unats :: "nat \<Rightarrow> nat set"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   139
  where "unats n = {i. i < 2 ^ n}"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   140
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   141
definition norm_sint :: "nat \<Rightarrow> int \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   142
  where "norm_sint n w = (w + 2 ^ (n - 1)) mod 2 ^ n - 2 ^ (n - 1)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   143
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   144
definition scast :: "'a::len word \<Rightarrow> 'b::len word"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
   145
  \<comment> \<open>cast a word to a different length\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   146
  where "scast w = word_of_int (sint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   147
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
   148
definition ucast :: "'a::len word \<Rightarrow> 'b::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   149
  where "ucast w = word_of_int (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   150
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
   151
instantiation word :: (len) size
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   152
begin
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   153
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
   154
definition word_size: "size (w :: 'a word) = LENGTH('a)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   155
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   156
instance ..
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   157
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   158
end
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   159
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   160
lemma word_size_gt_0 [iff]: "0 < size w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   161
  for w :: "'a::len word"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   162
  by (simp add: word_size)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   163
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   164
lemmas lens_gt_0 = word_size_gt_0 len_gt_0
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   165
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   166
lemma lens_not_0 [iff]:
71942
d2654b30f7bd eliminated warnings
haftmann
parents: 71826
diff changeset
   167
  \<open>size w \<noteq> 0\<close> for  w :: \<open>'a::len word\<close>
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   168
  by auto
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   169
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
   170
definition source_size :: "('a::len word \<Rightarrow> 'b) \<Rightarrow> nat"
67443
3abf6a722518 standardized towards new-style formal comments: isabelle update_comments;
wenzelm
parents: 67408
diff changeset
   171
  \<comment> \<open>whether a cast (or other) function is to a longer or shorter length\<close>
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   172
  where [code del]: "source_size c = (let arb = undefined; x = c arb in size arb)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   173
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
   174
definition target_size :: "('a \<Rightarrow> 'b::len word) \<Rightarrow> nat"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   175
  where [code del]: "target_size c = size (c undefined)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   176
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   177
definition is_up :: "('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   178
  where "is_up c \<longleftrightarrow> source_size c \<le> target_size c"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   179
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
   180
definition is_down :: "('a::len word \<Rightarrow> 'b::len word) \<Rightarrow> bool"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   181
  where "is_down c \<longleftrightarrow> target_size c \<le> source_size c"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   182
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
   183
definition of_bl :: "bool list \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   184
  where "of_bl bl = word_of_int (bl_to_bin bl)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   185
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
   186
definition to_bl :: "'a::len word \<Rightarrow> bool list"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   187
  where "to_bl w = bin_to_bl (LENGTH('a)) (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   188
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
   189
definition word_reverse :: "'a::len word \<Rightarrow> 'a word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   190
  where "word_reverse w = of_bl (rev (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   191
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
   192
definition word_int_case :: "(int \<Rightarrow> 'b) \<Rightarrow> 'a::len word \<Rightarrow> 'b"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   193
  where "word_int_case f w = f (uint w)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   194
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   195
translations
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   196
  "case x of XCONST of_int y \<Rightarrow> b" \<rightleftharpoons> "CONST word_int_case (\<lambda>y. b) x"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   197
  "case x of (XCONST of_int :: 'a) y \<Rightarrow> b" \<rightharpoonup> "CONST word_int_case (\<lambda>y. b) x"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   198
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   199
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   200
subsection \<open>Basic code generation setup\<close>
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   201
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
   202
definition Word :: "int \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   203
  where [code_post]: "Word = word_of_int"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   204
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   205
lemma [code abstype]: "Word (uint w) = w"
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   206
  by (simp add: Word_def word_of_int_uint)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   207
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   208
declare uint_word_of_int [code abstract]
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   209
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
   210
instantiation word :: (len) equal
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   211
begin
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   212
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   213
definition equal_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   214
  where "equal_word k l \<longleftrightarrow> HOL.equal (uint k) (uint l)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   215
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   216
instance
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   217
  by standard (simp add: equal equal_word_def word_uint_eq_iff)
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   218
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   219
end
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   220
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   221
notation fcomp (infixl "\<circ>>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   222
notation scomp (infixl "\<circ>\<rightarrow>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   223
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
   224
instantiation word :: ("{len, typerep}") random
55817
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   225
begin
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   226
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   227
definition
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   228
  "random_word i = Random.range i \<circ>\<rightarrow> (\<lambda>k. Pair (
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   229
     let j = word_of_int (int_of_integer (integer_of_natural k)) :: 'a word
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   230
     in (j, \<lambda>_::unit. Code_Evaluation.term_of j)))"
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   231
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   232
instance ..
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   233
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   234
end
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   235
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   236
no_notation fcomp (infixl "\<circ>>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   237
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   238
0bc0217387a5 earlier setup of transfer, without dependency on psychodelic interpretations
haftmann
parents: 55816
diff changeset
   239
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   240
subsection \<open>Type-definition locale instantiations\<close>
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   241
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   242
lemmas uint_0 = uint_nonnegative (* FIXME duplicate *)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   243
lemmas uint_lt = uint_bounded (* FIXME duplicate *)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   244
lemmas uint_mod_same = uint_idem (* FIXME duplicate *)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   245
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   246
lemma td_ext_uint:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   247
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   248
    (\<lambda>w::int. w mod 2 ^ LENGTH('a))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   249
  apply (unfold td_ext_def')
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   250
  apply transfer
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   251
  apply (simp add: uints_num take_bit_eq_mod)
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   252
  done
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   253
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   254
interpretation word_uint:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   255
  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
   256
    "uint::'a::len word \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   257
    word_of_int
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   258
    "uints (LENGTH('a::len))"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   259
    "\<lambda>w. w mod 2 ^ LENGTH('a::len)"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   260
  by (fact td_ext_uint)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   261
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   262
lemmas td_uint = word_uint.td_thm
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   263
lemmas int_word_uint = word_uint.eq_norm
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   264
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   265
lemma td_ext_ubin:
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   266
  "td_ext (uint :: 'a word \<Rightarrow> int) word_of_int (uints (LENGTH('a::len)))
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   267
    (bintrunc (LENGTH('a)))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   268
  by (unfold no_bintr_alt1) (fact td_ext_uint)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   269
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   270
interpretation word_ubin:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   271
  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
   272
    "uint::'a::len word \<Rightarrow> int"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   273
    word_of_int
71954
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   274
    "uints (LENGTH('a::len))"
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   275
    "bintrunc (LENGTH('a::len))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   276
  by (fact td_ext_ubin)
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   277
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
   278
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   279
subsection \<open>Arithmetic operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   280
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
   281
lift_definition word_succ :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x + 1"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   282
  by (auto simp add: bintrunc_mod2p intro: mod_add_cong)
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   283
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
   284
lift_definition word_pred :: "'a::len word \<Rightarrow> 'a word" is "\<lambda>x. x - 1"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   285
  by (auto simp add: bintrunc_mod2p intro: mod_diff_cong)
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   286
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
   287
instantiation word :: (len) "{neg_numeral, modulo, comm_monoid_mult, comm_ring}"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   288
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   289
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   290
lift_definition zero_word :: "'a word" is "0" .
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   291
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   292
lift_definition one_word :: "'a word" is "1" .
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   293
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
   294
lift_definition plus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(+)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   295
  by (auto simp add: bintrunc_mod2p intro: mod_add_cong)
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   296
67399
eab6ce8368fa ran isabelle update_op on all sources
nipkow
parents: 67122
diff changeset
   297
lift_definition minus_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(-)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   298
  by (auto simp add: bintrunc_mod2p intro: mod_diff_cong)
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   299
47387
a0f257197741 remove now-unnecessary type annotations from lift_definition commands
huffman
parents: 47377
diff changeset
   300
lift_definition uminus_word :: "'a word \<Rightarrow> 'a word" is uminus
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   301
  by (auto simp add: bintrunc_mod2p intro: mod_minus_cong)
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   302
69064
5840724b1d71 Prefix form of infix with * on either side no longer needs special treatment
nipkow
parents: 68157
diff changeset
   303
lift_definition times_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word" is "(*)"
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64243
diff changeset
   304
  by (auto simp add: bintrunc_mod2p intro: mod_mult_cong)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   305
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   306
lift_definition divide_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   307
  is "\<lambda>a b. take_bit LENGTH('a) a div take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   308
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   309
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   310
lift_definition modulo_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> 'a word"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   311
  is "\<lambda>a b. take_bit LENGTH('a) a mod take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   312
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   313
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   314
instance
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 61076
diff changeset
   315
  by standard (transfer, simp add: algebra_simps)+
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   316
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   317
end
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   318
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   319
lemma word_div_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   320
  "a div b = word_of_int (uint a div uint b)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   321
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   322
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   323
lemma word_mod_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   324
  "a mod b = word_of_int (uint a mod uint b)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   325
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   326
70901
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   327
quickcheck_generator word
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   328
  constructors:
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   329
    "zero_class.zero :: ('a::len) word",
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   330
    "numeral :: num \<Rightarrow> ('a::len) word",
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   331
    "uminus :: ('a::len) word \<Rightarrow> ('a::len) word"
94a0c47b8553 moved quickcheck setup to distribution
haftmann
parents: 70900
diff changeset
   332
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   333
context
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   334
  includes lifting_syntax
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   335
  notes power_transfer [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   336
begin
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   337
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   338
lemma power_transfer_word [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   339
  \<open>(pcr_word ===> (=) ===> pcr_word) (^) (^)\<close>
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   340
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   341
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   342
end
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   343
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   344
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   345
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   346
text \<open>Legacy theorems:\<close>
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   347
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   348
lemma word_arith_wis [code]:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   349
  shows word_add_def: "a + b = word_of_int (uint a + uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   350
    and word_sub_wi: "a - b = word_of_int (uint a - uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   351
    and word_mult_def: "a * b = word_of_int (uint a * uint b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   352
    and word_minus_def: "- a = word_of_int (- uint a)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   353
    and word_succ_alt: "word_succ a = word_of_int (uint a + 1)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   354
    and word_pred_alt: "word_pred a = word_of_int (uint a - 1)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   355
    and word_0_wi: "0 = word_of_int 0"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   356
    and word_1_wi: "1 = word_of_int 1"
71948
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   357
         apply (simp_all flip: plus_word.abs_eq minus_word.abs_eq
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   358
           times_word.abs_eq uminus_word.abs_eq
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   359
           zero_word.abs_eq one_word.abs_eq)
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   360
   apply transfer
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   361
   apply simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   362
  apply transfer
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   363
  apply simp
6ede899d26d3 fundamental construction of word type following existing transfer rules
haftmann
parents: 71947
diff changeset
   364
  done
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   365
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   366
lemma wi_homs:
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   367
  shows wi_hom_add: "word_of_int a + word_of_int b = word_of_int (a + b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   368
    and wi_hom_sub: "word_of_int a - word_of_int b = word_of_int (a - b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   369
    and wi_hom_mult: "word_of_int a * word_of_int b = word_of_int (a * b)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   370
    and wi_hom_neg: "- word_of_int a = word_of_int (- a)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   371
    and wi_hom_succ: "word_succ (word_of_int a) = word_of_int (a + 1)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   372
    and wi_hom_pred: "word_pred (word_of_int a) = word_of_int (a - 1)"
47374
9475d524bafb set up and use lift_definition for word operations
huffman
parents: 47372
diff changeset
   373
  by (transfer, simp)+
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   374
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   375
lemmas wi_hom_syms = wi_homs [symmetric]
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   376
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   377
lemmas word_of_int_homs = wi_homs word_0_wi word_1_wi
46009
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   378
5cb7ef5bfef2 remove duplicate lemma lists
huffman
parents: 46001
diff changeset
   379
lemmas word_of_int_hom_syms = word_of_int_homs [symmetric]
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   380
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
   381
instance word :: (len) comm_monoid_add ..
13bb3f5cdc5b pragmatically ruled out word types of length zero: a bit string with no bits is not bit string at all
haftmann
parents: 71953
diff changeset
   382
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
   383
instance word :: (len) semiring_numeral ..
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   384
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   385
instance word :: (len) comm_ring_1
45810
024947a0e492 prove class instances without extra lemmas
huffman
parents: 45809
diff changeset
   386
proof
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
   387
  have *: "0 < LENGTH('a)" by (rule len_gt_0)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   388
  show "(0::'a word) \<noteq> 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   389
    by transfer (use * in \<open>auto simp add: gr0_conv_Suc\<close>)
45810
024947a0e492 prove class instances without extra lemmas
huffman
parents: 45809
diff changeset
   390
qed
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   391
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   392
lemma word_of_nat: "of_nat n = word_of_int (int n)"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   393
  by (induct n) (auto simp add : word_of_int_hom_syms)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   394
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   395
lemma word_of_int: "of_int = word_of_int"
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   396
  apply (rule ext)
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   397
  apply (case_tac x rule: int_diff_cases)
46013
d2f179d26133 remove some duplicate lemmas
huffman
parents: 46012
diff changeset
   398
  apply (simp add: word_of_nat wi_hom_sub)
45545
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   399
  done
26aebb8ac9c1 Word.thy: rearrange to instantiate arithmetic classes together with arithmetic operations
huffman
parents: 45544
diff changeset
   400
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   401
context
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   402
  includes lifting_syntax
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   403
  notes 
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   404
    transfer_rule_of_bool [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   405
    transfer_rule_numeral [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   406
    transfer_rule_of_nat [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   407
    transfer_rule_of_int [transfer_rule]
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   408
begin
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   409
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   410
lemma [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   411
  "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) of_bool of_bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   412
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   413
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   414
lemma [transfer_rule]:
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
   415
  "((=) ===> (pcr_word :: int \<Rightarrow> 'a::len word \<Rightarrow> bool)) numeral numeral"
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   416
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   417
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   418
lemma [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   419
  "((=) ===> pcr_word) int of_nat"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   420
  by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   421
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   422
lemma [transfer_rule]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   423
  "((=) ===> pcr_word) (\<lambda>k. k) of_int"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   424
proof -
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   425
  have "((=) ===> pcr_word) of_int of_int"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   426
    by transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   427
  then show ?thesis by (simp add: id_def)
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   428
qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   429
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   430
end
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   431
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   432
lemma word_of_int_eq:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   433
  "word_of_int = of_int"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   434
  by (rule ext) (transfer, rule)
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   435
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   436
definition udvd :: "'a::len word \<Rightarrow> 'a::len word \<Rightarrow> bool" (infixl "udvd" 50)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   437
  where "a udvd b = (\<exists>n\<ge>0. uint b = n * uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   438
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   439
context
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   440
  includes lifting_syntax
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   441
begin
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   442
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   443
lemma [transfer_rule]:
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   444
  \<open>(pcr_word ===> (\<longleftrightarrow>)) even ((dvd) 2 :: 'a::len word \<Rightarrow> bool)\<close>
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   445
proof -
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   446
  have even_word_unfold: "even k \<longleftrightarrow> (\<exists>l. take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l))" (is "?P \<longleftrightarrow> ?Q")
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   447
    for k :: int
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   448
  proof
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   449
    assume ?P
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   450
    then show ?Q
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   451
      by auto
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   452
  next
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   453
    assume ?Q
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   454
    then obtain l where "take_bit LENGTH('a) k = take_bit LENGTH('a) (2 * l)" ..
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   455
    then have "even (take_bit LENGTH('a) k)"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   456
      by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   457
    then show ?P
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   458
      by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   459
  qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   460
  show ?thesis by (simp only: even_word_unfold [abs_def] dvd_def [where ?'a = "'a word", abs_def])
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   461
    transfer_prover
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   462
qed
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   463
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   464
end
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   465
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   466
instance word :: (len) semiring_modulo
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   467
proof
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   468
  show "a div b * b + a mod b = a" for a b :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   469
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   470
    fix k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   471
    define r :: int where "r = 2 ^ LENGTH('a)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   472
    then have r: "take_bit LENGTH('a) k = k mod r" for k
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   473
      by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   474
    have "k mod r = ((k mod r) div (l mod r) * (l mod r)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   475
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   476
      by (simp add: div_mult_mod_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   477
    also have "... = (((k mod r) div (l mod r) * (l mod r)) mod r
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   478
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   479
      by (simp add: mod_add_left_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   480
    also have "... = (((k mod r) div (l mod r) * l) mod r
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   481
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   482
      by (simp add: mod_mult_right_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   483
    finally have "k mod r = ((k mod r) div (l mod r) * l
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   484
      + (k mod r) mod (l mod r)) mod r"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   485
      by (simp add: mod_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   486
    with r show "take_bit LENGTH('a) (take_bit LENGTH('a) k div take_bit LENGTH('a) l * l
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   487
      + take_bit LENGTH('a) k mod take_bit LENGTH('a) l) = take_bit LENGTH('a) k"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   488
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   489
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   490
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   491
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   492
instance word :: (len) semiring_parity
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   493
proof
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   494
  show "\<not> 2 dvd (1::'a word)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   495
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   496
  show even_iff_mod_2_eq_0: "2 dvd a \<longleftrightarrow> a mod 2 = 0"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   497
    for a :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   498
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   499
  show "\<not> 2 dvd a \<longleftrightarrow> a mod 2 = 1"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   500
    for a :: "'a word"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   501
    by transfer (simp_all add: mod_2_eq_odd take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   502
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   503
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   504
lemma exp_eq_zero_iff:
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   505
  \<open>2 ^ n = (0 :: 'a::len word) \<longleftrightarrow> n \<ge> LENGTH('a)\<close>
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   506
  by transfer simp
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   507
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   508
lemma double_eq_zero_iff:
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   509
  \<open>2 * a = 0 \<longleftrightarrow> a = 0 \<or> a = 2 ^ (LENGTH('a) - Suc 0)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   510
  for a :: \<open>'a::len word\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   511
proof -
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   512
  define n where \<open>n = LENGTH('a) - Suc 0\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   513
  then have *: \<open>LENGTH('a) = Suc n\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   514
    by simp
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   515
  have \<open>a = 0\<close> if \<open>2 * a = 0\<close> and \<open>a \<noteq> 2 ^ (LENGTH('a) - Suc 0)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   516
    using that by transfer
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   517
      (auto simp add: take_bit_eq_0_iff take_bit_eq_mod *)
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   518
  moreover have \<open>2 ^ LENGTH('a) = (0 :: 'a word)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   519
    by transfer simp
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   520
  then have \<open>2 * 2 ^ (LENGTH('a) - Suc 0) = (0 :: 'a word)\<close>
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   521
    by (simp add: *)
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   522
  ultimately show ?thesis
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   523
    by auto
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   524
qed
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   525
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   526
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   527
subsection \<open>Ordering\<close>
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   528
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
   529
instantiation word :: (len) linorder
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   530
begin
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   531
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   532
lift_definition less_eq_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   533
  is "\<lambda>a b. take_bit LENGTH('a) a \<le> take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   534
  by simp
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   535
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   536
lift_definition less_word :: "'a word \<Rightarrow> 'a word \<Rightarrow> bool"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   537
  is "\<lambda>a b. take_bit LENGTH('a) a < take_bit LENGTH('a) b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   538
  by simp
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   539
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   540
instance
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   541
  by (standard; transfer) auto
45547
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   542
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   543
end
94c37f3df10f HOL-Word: removed more duplicate theorems
huffman
parents: 45546
diff changeset
   544
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   545
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
   546
  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
   547
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   548
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
   549
  by (standard; transfer) simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   550
71950
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   551
lemma word_le_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   552
  "a \<le> b \<longleftrightarrow> uint a \<le> uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   553
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   554
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   555
lemma word_less_def [code]:
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   556
  "a < b \<longleftrightarrow> uint a < uint b"
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   557
  by transfer rule
c9251bc7da4e more transfer rules
haftmann
parents: 71949
diff changeset
   558
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   559
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
   560
  \<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
   561
  by transfer (simp add: less_le)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   562
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   563
lemma of_nat_word_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   564
  \<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
   565
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   566
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   567
lemma of_nat_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   568
  \<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
   569
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   570
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   571
lemma of_nat_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   572
  \<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
   573
  by transfer (simp add: take_bit_of_nat)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   574
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   575
lemma of_nat_word_eq_0_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   576
  \<open>of_nat n = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   577
  using of_nat_word_eq_iff [where ?'a = 'a, of n 0] by (simp add: take_bit_eq_0_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   578
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   579
lemma of_int_word_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   580
  \<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
   581
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   582
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   583
lemma of_int_word_less_eq_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   584
  \<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
   585
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   586
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   587
lemma of_int_word_less_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   588
  \<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
   589
  by transfer rule
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   590
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   591
lemma of_int_word_eq_0_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   592
  \<open>of_int k = (0 :: 'a::len word) \<longleftrightarrow> 2 ^ LENGTH('a) dvd k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   593
  using of_int_word_eq_iff [where ?'a = 'a, of k 0] by (simp add: take_bit_eq_0_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   594
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   595
definition word_sle :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool"  ("(_/ <=s _)" [50, 51] 50)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   596
  where "a <=s b \<longleftrightarrow> sint a \<le> sint b"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   597
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   598
definition word_sless :: "'a::len word \<Rightarrow> 'a word \<Rightarrow> bool"  ("(_/ <s _)" [50, 51] 50)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   599
  where "x <s y \<longleftrightarrow> x <=s y \<and> x \<noteq> y"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   600
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   601
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
   602
subsection \<open>Bit-wise operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   603
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   604
lemma word_bit_induct [case_names zero even odd]:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   605
  \<open>P a\<close> if word_zero: \<open>P 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   606
    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
   607
    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
   608
  for P and a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   609
proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   610
  define m :: nat where \<open>m = LENGTH('a) - 1\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   611
  then have l: \<open>LENGTH('a) = Suc m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   612
    by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   613
  define n :: nat where \<open>n = unat a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   614
  then have \<open>n < 2 ^ LENGTH('a)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   615
    by (unfold unat_def) (transfer, simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   616
  then have \<open>n < 2 * 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   617
    by (simp add: l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   618
  then have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   619
  proof (induction n rule: nat_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   620
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   621
    show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   622
      by simp (rule word_zero)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   623
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   624
    case (even n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   625
    then have \<open>n < 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   626
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   627
    with even.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   628
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   629
    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
   630
      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
   631
    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
   632
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   633
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   634
    ultimately have \<open>P (2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   635
      by (rule word_even)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   636
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   637
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   638
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   639
    case (odd n)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   640
    then have \<open>Suc n \<le> 2 ^ m\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   641
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   642
    with odd.IH have \<open>P (of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   643
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   644
    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
   645
      using of_nat_word_less_iff [where ?'a = 'a, of n \<open>2 ^ m\<close>]
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   646
      by (cases \<open>m = 0\<close>) (simp_all add: not_less take_bit_eq_self ac_simps l)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   647
    ultimately have \<open>P (1 + 2 * of_nat n)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   648
      by (rule word_odd)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   649
    then show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   650
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   651
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   652
  moreover have \<open>of_nat (nat (uint a)) = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   653
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   654
  ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   655
    by (simp add: n_def unat_def)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   656
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   657
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   658
lemma bit_word_half_eq:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   659
  \<open>(of_bool b + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   660
    if \<open>a < 2 ^ (LENGTH('a) - Suc 0)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   661
    for a :: \<open>'a::len word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   662
proof (cases \<open>2 \<le> LENGTH('a::len)\<close>)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   663
  case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   664
  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
   665
    by auto
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   666
  with False that show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   667
    by transfer (simp add: eq_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   668
next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   669
  case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   670
  obtain n where length: \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   671
    by (cases \<open>LENGTH('a)\<close>) simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   672
  show ?thesis proof (cases b)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   673
    case False
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   674
    moreover have \<open>a * 2 div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   675
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   676
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   677
      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
   678
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   679
      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
   680
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   681
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   682
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   683
      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
   684
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   685
      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
   686
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   687
        by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   688
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   689
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   690
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   691
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   692
    case True
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   693
    moreover have \<open>(1 + a * 2) div 2 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   694
    using that proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   695
      fix k :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   696
      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
   697
        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
   698
      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
   699
      with \<open>LENGTH('a) = Suc n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   700
      have \<open>k mod 2 ^ LENGTH('a) = k mod 2 ^ n\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   701
        by (simp add: take_bit_eq_mod divmod_digit_0)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   702
      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
   703
        by (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   704
      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
   705
        = take_bit LENGTH('a) k\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   706
        by (auto simp add: take_bit_Suc)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   707
    qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   708
    ultimately show ?thesis
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   709
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   710
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   711
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   712
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   713
lemma even_mult_exp_div_word_iff:
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   714
  \<open>even (a * 2 ^ m div 2 ^ n) \<longleftrightarrow> \<not> (
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   715
    m \<le> n \<and>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   716
    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
   717
  by transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   718
    (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
   719
      simp_all flip: push_bit_eq_mult add: bit_push_bit_iff_int)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   720
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   721
instantiation word :: (len) semiring_bits
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   722
begin
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   723
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   724
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
   725
  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
   726
proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   727
  fix k l :: int and n :: nat
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   728
  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
   729
  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
   730
  proof (cases \<open>n < LENGTH('a)\<close>)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   731
    case True
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   732
    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
   733
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   734
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   735
      by (simp add: bit_take_bit_iff)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   736
  next
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   737
    case False
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   738
    then show ?thesis
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   739
      by simp
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   740
  qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   741
qed
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   742
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   743
instance proof
71951
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   744
  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
   745
    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
   746
  for P and a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   747
  proof (induction a rule: word_bit_induct)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   748
    case zero
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   749
    have \<open>0 div 2 = (0::'a word)\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   750
      by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   751
    with stable [of 0] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   752
      by simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   753
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   754
    case (even a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   755
    with rec [of a False] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   756
      using bit_word_half_eq [of a False] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   757
  next
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   758
    case (odd a)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   759
    with rec [of a True] show ?case
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   760
      using bit_word_half_eq [of a True] by (simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   761
  qed
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   762
  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
   763
    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
   764
  show \<open>0 div a = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   765
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   766
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   767
  show \<open>a div 1 = a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   768
    for a :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   769
    by transfer simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   770
  show \<open>a mod b div b = 0\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   771
    for a b :: \<open>'a word\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   772
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   773
    apply (simp add: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   774
    apply (subst (3) mod_pos_pos_trivial [of _ \<open>2 ^ LENGTH('a)\<close>])
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   775
      apply simp_all
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   776
     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
   777
    using pos_mod_bound [of \<open>2 ^ LENGTH('a)\<close>] apply simp
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   778
  proof -
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   779
    fix aa :: int and ba :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   780
    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
   781
      by (metis le_less take_bit_eq_mod take_bit_nonnegative)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   782
    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
   783
      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
   784
    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
   785
      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
   786
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   787
  show \<open>(1 + a) div 2 = a div 2\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   788
    if \<open>even a\<close>
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   789
    for a :: \<open>'a word\<close>
71953
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   790
    using that by transfer
428609096812 more lemmas and less name space pollution
haftmann
parents: 71952
diff changeset
   791
      (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
   792
  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
   793
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   794
    by transfer (simp, simp add: exp_div_exp_eq)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   795
  show "a div 2 ^ m div 2 ^ n = a div 2 ^ (m + n)"
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   796
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   797
    apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   798
    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
   799
    apply (simp add: drop_bit_take_bit)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   800
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   801
  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
   802
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   803
    by transfer (auto simp flip: take_bit_eq_mod simp add: ac_simps)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   804
  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
   805
    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
   806
    using that apply transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   807
    apply (auto simp flip: take_bit_eq_mod)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   808
           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
   809
    done
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   810
  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
   811
    for a :: "'a word" and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   812
    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
   813
  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
   814
    for m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   815
    by transfer (auto simp add: take_bit_of_mask even_mask_div_iff)
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   816
  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
   817
    for a :: \<open>'a word\<close> and m n :: nat
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   818
  proof transfer
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   819
    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
   820
      n < m
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   821
      \<or> take_bit LENGTH('a) ((2::int) ^ n) = take_bit LENGTH('a) 0
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   822
      \<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
   823
    for m n :: nat and k l :: int
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   824
      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
   825
        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
   826
  qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   827
qed
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   828
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   829
end
ac6f9738c200 essential instance about bit structure
haftmann
parents: 71950
diff changeset
   830
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   831
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
   832
begin
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   833
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   834
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
   835
  is push_bit
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   836
proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   837
  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
   838
    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
   839
  proof -
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   840
    from that
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   841
    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
   842
      = 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
   843
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   844
    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
   845
      by simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   846
    ultimately show ?thesis
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   847
      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
   848
  qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   849
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   850
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   851
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
   852
  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
   853
  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
   854
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   855
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
   856
  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
   857
  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
   858
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   859
instance proof
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
   860
  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
   861
    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
   862
  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
   863
    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
   864
  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
   865
    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
   866
qed
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   867
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   868
end
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   869
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   870
lemma bit_word_eqI:
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   871
  \<open>a = b\<close> if \<open>\<And>n. n \<le> LENGTH('a) \<Longrightarrow> bit a n \<longleftrightarrow> bit b n\<close>
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   872
  for a b :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   873
  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
   874
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   875
lemma bit_imp_le_length:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   876
  \<open>n < LENGTH('a)\<close> if \<open>bit w n\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   877
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   878
  using that by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   879
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   880
lemma not_bit_length [simp]:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   881
  \<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
   882
  by transfer simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   883
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   884
lemma bit_word_of_int_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   885
  \<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
   886
  by transfer rule
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   887
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   888
lemma bit_uint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   889
  \<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
   890
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   891
  by transfer (simp add: bit_take_bit_iff)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   892
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   893
lemma bit_sint_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   894
  \<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
   895
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   896
  apply (cases \<open>LENGTH('a)\<close>)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   897
   apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   898
  apply (simp add: sint_uint nth_sbintr not_less bit_uint_iff not_le Suc_le_eq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   899
  apply (auto simp add: le_less dest: bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   900
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   901
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   902
lemma bit_word_ucast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   903
  \<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
   904
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   905
  by (simp add: ucast_def bit_word_of_int_iff bit_uint_iff ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   906
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   907
lemma bit_word_scast_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   908
  \<open>bit (scast w :: 'b::len word) n \<longleftrightarrow>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   909
    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
   910
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   911
  by (simp add: scast_def bit_word_of_int_iff bit_sint_iff ac_simps)
71958
4320875eb8a1 more lemmas
haftmann
parents: 71957
diff changeset
   912
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
   913
definition shiftl1 :: "'a::len word \<Rightarrow> 'a word"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   914
  where "shiftl1 w = word_of_int (2 * uint w)"
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   915
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   916
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
   917
  \<open>shiftl1 = (*) 2\<close>
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71965
diff changeset
   918
  apply (simp add: fun_eq_iff shiftl1_def)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   919
  apply transfer
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   920
  apply (simp only: mult_2 take_bit_add)
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   921
  apply simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   922
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   923
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   924
lemma bit_shiftl1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   925
  \<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
   926
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   927
  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
   928
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
   929
definition shiftr1 :: "'a::len word \<Rightarrow> 'a word"
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   930
  \<comment> \<open>shift right as unsigned or as signed, ie logical or arithmetic\<close>
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   931
  where "shiftr1 w = word_of_int (bin_rest (uint w))"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   932
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   933
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
   934
  \<open>shiftr1 w = w div 2\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   935
  apply (simp add: fun_eq_iff shiftr1_def)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   936
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   937
  apply (auto simp add: not_le dest: less_2_cases)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   938
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   939
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   940
lemma bit_shiftr1_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   941
  \<open>bit (shiftr1 w) n \<longleftrightarrow> bit w (Suc n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   942
    for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   943
  by (simp add: shiftr1_eq_div_2 bit_Suc)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
   944
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   945
instantiation word :: (len) ring_bit_operations
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   946
begin
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   947
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   948
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
   949
  is not
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   950
  by (simp add: take_bit_not_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   951
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   952
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
   953
  is \<open>and\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   954
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   955
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   956
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
   957
  is or
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   958
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   959
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   960
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
   961
  is xor
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   962
  by simp
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   963
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   964
instance proof
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   965
  fix a b :: \<open>'a word\<close> and n :: nat
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   966
  show \<open>- a = NOT (a - 1)\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   967
    by transfer (simp add: minus_eq_not_minus_1)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   968
  show \<open>bit (NOT a) n \<longleftrightarrow> (2 :: 'a word) ^ n \<noteq> 0 \<and> \<not> bit a n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   969
    by transfer (simp add: bit_not_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   970
  show \<open>bit (a AND b) n \<longleftrightarrow> bit a n \<and> bit b n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   971
    by transfer (auto simp add: bit_and_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   972
  show \<open>bit (a OR b) n \<longleftrightarrow> bit a n \<or> bit b n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   973
    by transfer (auto simp add: bit_or_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   974
  show \<open>bit (a XOR b) n \<longleftrightarrow> bit a n \<noteq> bit b n\<close>
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   975
    by transfer (auto simp add: bit_xor_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   976
qed
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   977
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   978
end
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
   979
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
   980
instantiation word :: (len) bit_operations
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   981
begin
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   982
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   983
definition word_test_bit_def: "test_bit a = bin_nth (uint a)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   984
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   985
definition word_set_bit_def: "set_bit a n x = word_of_int (bin_sc n x (uint a))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   986
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   987
definition word_lsb_def: "lsb a \<longleftrightarrow> bin_last (uint a)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   988
70175
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
   989
definition "msb a \<longleftrightarrow> bin_sign (sbintrunc (LENGTH('a) - 1) (uint a)) = - 1"
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
   990
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   991
definition shiftl_def: "w << n = (shiftl1 ^^ n) w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   992
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   993
definition shiftr_def: "w >> n = (shiftr1 ^^ n) w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   994
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   995
instance ..
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   996
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   997
end
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
   998
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
   999
lemma test_bit_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1000
  \<open>test_bit w = bit w\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1001
  apply (simp add: word_test_bit_def fun_eq_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1002
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1003
  apply (simp add: bit_take_bit_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1004
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1005
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1006
lemma set_bit_unfold:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1007
  \<open>set_bit w n b = (if b then Bit_Operations.set_bit n w else unset_bit n w)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1008
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1009
  apply (auto simp add: word_set_bit_def bin_clr_conv_NAND bin_set_conv_OR unset_bit_def set_bit_def shiftl_int_def; transfer)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1010
   apply simp_all
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1011
  done
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_set_bit_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1014
  \<open>bit (set_bit w m b) n \<longleftrightarrow> (if m = n then n < LENGTH('a) \<and> b else bit w n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1015
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1016
  by (auto simp add: set_bit_unfold bit_unset_bit_iff bit_set_bit_iff exp_eq_zero_iff not_le bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1017
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1018
lemma lsb_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1019
  \<open>lsb = (odd :: 'a word \<Rightarrow> bool)\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1020
  apply (simp add: word_lsb_def fun_eq_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1021
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1022
  apply simp
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1023
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1024
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1025
lemma msb_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1026
  \<open>msb w \<longleftrightarrow> bit w (LENGTH('a) - 1)\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1027
  apply (simp add: msb_word_def bin_sign_lem)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1028
  apply transfer
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1029
  apply (simp add: bit_take_bit_iff)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1030
  done
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1031
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1032
lemma shiftl_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1033
  \<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1034
  by (induction n) (simp_all add: shiftl_def shiftl1_eq_mult_2 push_bit_double)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1035
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1036
lemma bit_shiftl_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1037
  \<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
  1038
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1039
  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
  1040
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1041
lemma [code]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1042
  \<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
  1043
  by (simp add: shiftl_word_eq)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1044
71952
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1045
lemma shiftr_word_eq:
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1046
  \<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close>
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1047
  by (induction n) (simp_all add: shiftr_def shiftr1_eq_div_2 drop_bit_Suc drop_bit_half)
2efc5b8c7456 canonical bit shifts for word type, leaving duplicates as they are at the moment
haftmann
parents: 71951
diff changeset
  1048
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1049
lemma bit_shiftr_word_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1050
  \<open>bit (w >> m) n \<longleftrightarrow> bit w (m + n)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1051
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1052
  by (simp add: shiftr_word_eq bit_drop_bit_eq)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1053
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1054
lemma [code]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1055
  \<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
  1056
  by (simp add: shiftr_word_eq)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1057
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1058
lemma [code]:
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1059
  \<open>take_bit n a = a AND Bit_Operations.mask n\<close> for a :: \<open>'a::len word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1060
  by (fact take_bit_eq_mask)
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1061
71955
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1062
lemma [code_abbrev]:
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1063
  \<open>push_bit n 1 = (2 :: 'a::len word) ^ n\<close>
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1064
  by (fact push_bit_of_1)
a9f913d17d00 tweak for code generation
haftmann
parents: 71954
diff changeset
  1065
70175
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1066
lemma word_msb_def:
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1067
  "msb a \<longleftrightarrow> bin_sign (sint a) = - 1"
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1068
  by (simp add: msb_word_def sint_uint)
85fb1a585f52 eliminated type class
haftmann
parents: 70173
diff changeset
  1069
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1070
lemma [code]:
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
  1071
  shows word_not_def: "NOT (a::'a::len word) = word_of_int (NOT (uint a))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1072
    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
  1073
    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
  1074
    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
  1075
  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
  1076
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
  1077
definition setBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1078
  where "setBit w n = set_bit w n True"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1079
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1080
lemma setBit_eq_set_bit:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1081
  \<open>setBit w n = Bit_Operations.set_bit n w\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1082
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1083
  by (simp add: setBit_def set_bit_unfold)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1084
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1085
lemma bit_setBit_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1086
  \<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
  1087
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1088
  by (simp add: setBit_eq_set_bit bit_set_bit_iff exp_eq_zero_iff not_le ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1089
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
  1090
definition clearBit :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1091
  where "clearBit w n = set_bit w n False"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1092
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1093
lemma clearBit_eq_unset_bit:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1094
  \<open>clearBit w n = unset_bit n w\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1095
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1096
  by (simp add: clearBit_def set_bit_unfold)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1097
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1098
lemma bit_clearBit_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1099
  \<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
  1100
  for w :: \<open>'a::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1101
  by (simp add: clearBit_eq_unset_bit bit_unset_bit_iff ac_simps)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1102
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1103
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
  1104
  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
  1105
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1106
lemma even_word_iff [code]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1107
  \<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
  1108
  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
  1109
71965
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1110
lemma bit_word_iff_drop_bit_and [code]:
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1111
  \<open>bit a n \<longleftrightarrow> drop_bit n a AND 1 = 1\<close> for a :: \<open>'a::len word\<close>
d45f5d4c41bd more class operations for the sake of efficient generated code
haftmann
parents: 71958
diff changeset
  1112
  by (simp add: bit_iff_odd_drop_bit odd_iff_mod_2_eq_one and_one_eq)
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71955
diff changeset
  1113
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1114
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1115
subsection \<open>Shift operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1116
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1117
definition sshiftr1 :: "'a::len word \<Rightarrow> 'a word"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1118
  where "sshiftr1 w = word_of_int (bin_rest (sint w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1119
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1120
definition bshiftr1 :: "bool \<Rightarrow> 'a::len word \<Rightarrow> 'a word"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1121
  where "bshiftr1 b w = of_bl (b # butlast (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1122
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1123
definition sshiftr :: "'a::len word \<Rightarrow> nat \<Rightarrow> 'a word"  (infixl ">>>" 55)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1124
  where "w >>> n = (sshiftr1 ^^ n) w"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1125
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1126
definition mask :: "nat \<Rightarrow> 'a::len word"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1127
  where "mask n = (1 << n) - 1"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1128
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1129
definition slice1 :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1130
  where "slice1 n w = of_bl (takefill False n (to_bl w))"
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1131
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
  1132
definition revcast :: "'a::len word \<Rightarrow> 'b::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1133
  where "revcast w =  of_bl (takefill False (LENGTH('b)) (to_bl w))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1134
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1135
lemma revcast_eq:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1136
  \<open>(revcast :: 'a::len word \<Rightarrow> 'b::len word) = slice1 LENGTH('b)\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1137
  by (simp add: fun_eq_iff revcast_def slice1_def)
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1138
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
  1139
definition slice :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'b::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1140
  where "slice n w = slice1 (size w - n) w"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1141
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1142
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1143
subsection \<open>Rotation\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1144
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1145
definition rotater1 :: "'a list \<Rightarrow> 'a list"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1146
  where "rotater1 ys =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1147
    (case ys of [] \<Rightarrow> [] | x # xs \<Rightarrow> last ys # butlast ys)"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1148
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1149
definition rotater :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1150
  where "rotater n = rotater1 ^^ n"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1151
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
  1152
definition word_rotr :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1153
  where "word_rotr n w = of_bl (rotater n (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1154
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
  1155
definition word_rotl :: "nat \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1156
  where "word_rotl n w = of_bl (rotate n (to_bl w))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1157
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
  1158
definition word_roti :: "int \<Rightarrow> 'a::len word \<Rightarrow> 'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1159
  where "word_roti i w =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1160
    (if i \<ge> 0 then word_rotr (nat i) w else word_rotl (nat (- i)) w)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1161
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1162
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1163
subsection \<open>Split and cat operations\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1164
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
  1165
definition word_cat :: "'a::len word \<Rightarrow> 'b::len word \<Rightarrow> 'c::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1166
  where "word_cat a b = word_of_int (bin_cat (uint a) (LENGTH('b)) (uint b))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1167
71990
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1168
lemma word_cat_eq:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1169
  \<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
  1170
  for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1171
  apply (simp add: word_cat_def bin_cat_eq_push_bit_add_take_bit ucast_def)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1172
  apply transfer apply simp
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1173
  done
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1174
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1175
lemma bit_word_cat_iff:
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1176
  \<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
  1177
  for v :: \<open>'a::len word\<close> and w :: \<open>'b::len word\<close>
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1178
  by (auto simp add: word_cat_def bit_word_of_int_iff bin_nth_cat bit_uint_iff not_less bit_imp_le_length)
66beb9d92e43 explicit proofs for bit projections
haftmann
parents: 71986
diff changeset
  1179
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
  1180
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
  1181
  where "word_split a =
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1182
    (case bin_split (LENGTH('c)) (uint a) of
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1183
      (u, v) \<Rightarrow> (word_of_int u, word_of_int v))"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1184
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
  1185
definition word_rcat :: "'a::len word list \<Rightarrow> 'b::len word"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1186
  where "word_rcat ws = word_of_int (bin_rcat (LENGTH('a)) (map uint ws))"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1187
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
  1188
definition word_rsplit :: "'a::len word \<Rightarrow> 'b::len word list"
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1189
  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
  1190
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
  1191
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
  1192
  \<comment> \<open>Largest representable machine integer.\<close>
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
  1193
  where "max_word \<equiv> - 1"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1194
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1195
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61649
diff changeset
  1196
subsection \<open>Theorems about typedefs\<close>
46010
ebbc2d5cd720 add section headings
huffman
parents: 46009
diff changeset
  1197
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1198
lemma sint_sbintrunc': "sint (word_of_int bin :: 'a word) = sbintrunc (LENGTH('a::len) - 1) bin"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1199
  by (auto simp: sint_uint word_ubin.eq_norm sbintrunc_bintrunc_lt)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1200
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1201
lemma uint_sint: "uint w = bintrunc (LENGTH('a)) (sint w)"
65328
2510b0ce28da misc tuning and modernization;
wenzelm
parents: 65268
diff changeset
  1202
  for w :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1203
  by (auto simp: sint_uint bintrunc_sbintrunc_le)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1204
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1205
lemma bintr_uint: "LENGTH('a) \<le> n \<Longrightarrow> bintrunc 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
  1206
  for w :: "'a::len word"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1207
  apply (subst word_ubin.norm_Rep [symmetric])
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1208
  apply (simp only: bintrunc_bintrunc_min word_size)
54863
82acc20ded73 prefer more canonical names for lemmas on min/max
haftmann
parents: 54854
diff changeset
  1209
  apply (simp add: min.absorb2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1210
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1211
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1212
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
  1213
  "LENGTH('a::len) \<le> n \<Longrightarrow>
46057
8664713db181 remove unnecessary intermediate lemmas
huffman
parents: 46026
diff changeset
  1214
    word_of_int (bintrunc n w) = (word_of_int w :: 'a word)"
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1215
  by (auto simp: word_ubin.norm_eq_iff [symmetric] min.absorb1)
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1216
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1217
lemma td_ext_sbin:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1218
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1219
    (sbintrunc (LENGTH('a) - 1))"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1220
  apply (unfold td_ext_def' sint_uint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1221
  apply (simp add : word_ubin.eq_norm)
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1222
  apply (cases "LENGTH('a)")
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1223
   apply (auto simp add : sints_def)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1224
  apply (rule sym [THEN trans])
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1225
   apply (rule word_ubin.Abs_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1226
  apply (simp only: bintrunc_sbintrunc)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1227
  apply (drule sym)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1228
  apply simp
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1229
  done
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1230
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1231
lemma td_ext_sint:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1232
  "td_ext (sint :: 'a word \<Rightarrow> int) word_of_int (sints (LENGTH('a::len)))
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1233
     (\<lambda>w. (w + 2 ^ (LENGTH('a) - 1)) mod 2 ^ LENGTH('a) -
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1234
         2 ^ (LENGTH('a) - 1))"
55816
e8dd03241e86 cursory polishing: tuned proofs, tuned symbols, tuned headings
haftmann
parents: 55415
diff changeset
  1235
  using td_ext_sbin [where ?'a = 'a] by (simp add: no_sbintr_alt2)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1236
67408
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1237
text \<open>
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1238
  We do \<open>sint\<close> before \<open>sbin\<close>, before \<open>sint\<close> is the user version
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1239
  and interpretations do not produce thm duplicates. I.e.
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1240
  we get the name \<open>word_sint.Rep_eqD\<close>, but not \<open>word_sbin.Req_eqD\<close>,
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1241
  because the latter is the same thm as the former.
4a4c14b24800 prefer formal comments;
wenzelm
parents: 67399
diff changeset
  1242
\<close>
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1243
interpretation word_sint:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1244
  td_ext
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1245
    "sint ::'a::len word \<Rightarrow> int"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1246
    word_of_int
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1247
    "sints (LENGTH('a::len))"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1248
    "\<lambda>w. (w + 2^(LENGTH('a::len) - 1)) mod 2^LENGTH('a::len) -
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1249
      2 ^ (LENGTH('a::len) - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1250
  by (rule td_ext_sint)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1251
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1252
interpretation word_sbin:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1253
  td_ext
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1254
    "sint ::'a::len word \<Rightarrow> int"
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1255
    word_of_int
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1256
    "sints (LENGTH('a::len))"
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset
  1257
    "sbintrunc (LENGTH('a::len) - 1)"
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1258
  by (rule td_ext_sbin)
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1259
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45550
diff changeset
  1260
lemmas int_word_sint = td_ext_sint [THEN td_ext.eq_norm]
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1261
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1262
lemmas td_sint = word_sint.td
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1263
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
  1264
lemma to_bl_def': "(to_bl :: 'a::len word \<Rightarrow> bool list) = bin_to_bl (LENGTH('a)) \<circ> uint"
44762
8f9d09241a68 tuned proofs;
wenzelm
parents: 42793
diff changeset
  1265
  by (auto simp: to_bl_def)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1266
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1267
lemmas word_reverse_no_def [simp] =
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1268
  word_reverse_def [of "numeral w"] for w
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1269
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1270
lemma uints_mod: "uints n = range (\<lambda>w. w mod 2 ^ n)"
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1271
  by (fact uints_def [unfolded no_bintr_alt1])
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1272
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1273
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
  1274
  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
  1275
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1276
declare word_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1277
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1278
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
  1279
  by (simp only: word_numeral_alt wi_hom_neg)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1280
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1281
declare word_neg_numeral_alt [symmetric, code_abbrev]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1282
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1283
lemma uint_bintrunc [simp]:
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1284
  "uint (numeral bin :: 'a word) =
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
  1285
    bintrunc (LENGTH('a::len)) (numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1286
  unfolding word_numeral_alt by (rule word_ubin.eq_norm)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1287
65268
75f2aa8ecb12 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1288
lemma uint_bintrunc_neg [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
  1289
  "uint (- numeral bin :: 'a word) = bintrunc (LENGTH('a::len)) (- numeral bin)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46962
diff changeset
  1290
  by (simp only: word_neg_numeral_alt word_ubin.eq_norm)
37660
56e3520b68b2 one unified Word theory
haftmann
parents: 36899
diff changeset
  1291
45805
3c609e8785f2 tidied Word.thy;
huffman
parents: 45804
diff changeset
  1292
lemma sint_sbintrunc [simp]:
70185
ac1706cdde25 clarified notation
haftmann
parents: 70183
diff changeset