src/HOL/Word/Traditional_Syntax.thy
author paulson
Mon, 30 Nov 2020 19:33:07 +0000
changeset 72796 d39a32cff5d7
parent 72514 d8661799afb2
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
     1
(*  Author:     Jeremy Dawson, NICTA
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
     2
*)
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
     3
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
     4
section \<open>Operation variants with traditional syntax\<close>
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
     5
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
     6
theory Traditional_Syntax
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
     7
  imports Word
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
     8
begin
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
     9
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    10
class semiring_bit_syntax = semiring_bit_shifts
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    11
begin
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    12
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    13
definition test_bit :: \<open>'a \<Rightarrow> nat \<Rightarrow> bool\<close>  (infixl "!!" 100)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    14
  where test_bit_eq_bit: \<open>test_bit = bit\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    15
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    16
definition shiftl :: \<open>'a \<Rightarrow> nat \<Rightarrow> 'a\<close>  (infixl "<<" 55)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    17
  where shiftl_eq_push_bit: \<open>a << n = push_bit n a\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    18
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    19
definition shiftr :: \<open>'a \<Rightarrow> nat \<Rightarrow> 'a\<close>  (infixl ">>" 55)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    20
  where shiftr_eq_drop_bit: \<open>a >> n = drop_bit n a\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    21
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    22
end
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    23
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    24
instance word :: (len) semiring_bit_syntax ..
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    25
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    26
context
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    27
  includes lifting_syntax
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
    28
begin
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
    29
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    30
lemma test_bit_word_transfer [transfer_rule]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    31
  \<open>(pcr_word ===> (=)) (\<lambda>k n. n < LENGTH('a) \<and> bit k n) (test_bit :: 'a::len word \<Rightarrow> _)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    32
  by (unfold test_bit_eq_bit) transfer_prover
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    33
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    34
lemma shiftl_word_transfer [transfer_rule]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    35
  \<open>(pcr_word ===> (=) ===> pcr_word) (\<lambda>k n. push_bit n k) shiftl\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    36
  by (unfold shiftl_eq_push_bit) transfer_prover
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    37
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    38
lemma shiftr_word_transfer [transfer_rule]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    39
  \<open>(pcr_word ===> (=) ===> pcr_word) (\<lambda>k n. (drop_bit n \<circ> take_bit LENGTH('a)) k) (shiftr :: 'a::len word \<Rightarrow> _)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    40
  by (unfold shiftr_eq_drop_bit) transfer_prover
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
    41
379d0c207c29 separation of traditional bit operations
haftmann
parents:
diff changeset
    42
end
72508
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    43
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    44
lemma test_bit_word_eq:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    45
  \<open>test_bit = (bit :: 'a::len word \<Rightarrow> _)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    46
  by (fact test_bit_eq_bit)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    47
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    48
lemma shiftl_word_eq:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    49
  \<open>w << n = push_bit n w\<close> for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    50
  by (fact shiftl_eq_push_bit)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    51
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    52
lemma shiftr_word_eq:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    53
  \<open>w >> n = drop_bit n w\<close> for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    54
  by (fact shiftr_eq_drop_bit)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    55
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    56
lemma test_bit_eq_iff: "test_bit u = test_bit v \<longleftrightarrow> u = v"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    57
  for u v :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    58
  by (simp add: bit_eq_iff test_bit_eq_bit fun_eq_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    59
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    60
lemma test_bit_size: "w !! n \<Longrightarrow> n < size w"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    61
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    62
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    63
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    64
lemma word_eq_iff: "x = y \<longleftrightarrow> (\<forall>n<LENGTH('a). x !! n = y !! n)" (is \<open>?P \<longleftrightarrow> ?Q\<close>)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    65
  for x y :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    66
  by transfer (auto simp add: bit_eq_iff bit_take_bit_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    67
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    68
lemma word_eqI: "(\<And>n. n < size u \<longrightarrow> u !! n = v !! n) \<Longrightarrow> u = v"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    69
  for u :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    70
  by (simp add: word_size word_eq_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    71
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    72
lemma word_eqD: "u = v \<Longrightarrow> u !! x = v !! x"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    73
  for u v :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    74
  by simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    75
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    76
lemma test_bit_bin': "w !! n \<longleftrightarrow> n < size w \<and> bit (uint w) n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    77
  by transfer (simp add: bit_take_bit_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    78
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    79
lemmas test_bit_bin = test_bit_bin' [unfolded word_size]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    80
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    81
lemma word_test_bit_def: 
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    82
  \<open>test_bit a = bit (uint a)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    83
  by transfer (simp add: fun_eq_iff bit_take_bit_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    84
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    85
lemmas test_bit_def' = word_test_bit_def [THEN fun_cong]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    86
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    87
lemma word_test_bit_transfer [transfer_rule]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    88
  "(rel_fun pcr_word (rel_fun (=) (=)))
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    89
    (\<lambda>x n. n < LENGTH('a) \<and> bit x n) (test_bit :: 'a::len word \<Rightarrow> _)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    90
  by (simp only: test_bit_eq_bit) transfer_prover
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    91
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    92
lemma test_bit_wi [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    93
  "(word_of_int x :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a) \<and> bit x n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    94
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    95
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    96
lemma word_ops_nth_size:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    97
  "n < size x \<Longrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    98
    (x OR y) !! n = (x !! n | y !! n) \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
    99
    (x AND y) !! n = (x !! n \<and> y !! n) \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   100
    (x XOR y) !! n = (x !! n \<noteq> y !! n) \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   101
    (NOT x) !! n = (\<not> x !! n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   102
  for x :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   103
  by transfer (simp add: bit_or_iff bit_and_iff bit_xor_iff bit_not_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   104
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   105
lemma word_ao_nth:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   106
  "(x OR y) !! n = (x !! n | y !! n) \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   107
    (x AND y) !! n = (x !! n \<and> y !! n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   108
  for x :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   109
  by transfer (auto simp add: bit_or_iff bit_and_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   110
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   111
lemmas msb0 = len_gt_0 [THEN diff_Suc_less, THEN word_ops_nth_size [unfolded word_size]]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   112
lemmas msb1 = msb0 [where i = 0]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   113
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   114
lemma test_bit_numeral [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   115
  "(numeral w :: 'a::len word) !! n \<longleftrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   116
    n < LENGTH('a) \<and> bit (numeral w :: int) n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   117
  by transfer (rule refl)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   118
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   119
lemma test_bit_neg_numeral [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   120
  "(- numeral w :: 'a::len word) !! n \<longleftrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   121
    n < LENGTH('a) \<and> bit (- numeral w :: int) n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   122
  by transfer (rule refl)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   123
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   124
lemma test_bit_1 [simp]: "(1 :: 'a::len word) !! n \<longleftrightarrow> n = 0"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   125
  by transfer (auto simp add: bit_1_iff) 
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   126
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   127
lemma nth_0 [simp]: "\<not> (0 :: 'a::len word) !! n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   128
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   129
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   130
lemma nth_minus1 [simp]: "(-1 :: 'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   131
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   132
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   133
lemma shiftl1_code [code]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   134
  \<open>shiftl1 w = push_bit 1 w\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   135
  by transfer (simp add: ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   136
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   137
lemma uint_shiftr_eq:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   138
  \<open>uint (w >> n) = uint w div 2 ^ n\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   139
  by transfer (simp flip: drop_bit_eq_div add: drop_bit_take_bit min_def le_less less_diff_conv)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   140
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   141
lemma shiftr1_code [code]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   142
  \<open>shiftr1 w = drop_bit 1 w\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   143
  by transfer (simp add: drop_bit_Suc)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   144
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   145
lemma shiftl_def:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   146
  \<open>w << n = (shiftl1 ^^ n) w\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   147
proof -
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   148
  have \<open>push_bit n = (((*) 2 ^^ n) :: int \<Rightarrow> int)\<close> for n
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   149
    by (induction n) (simp_all add: fun_eq_iff funpow_swap1, simp add: ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   150
  then show ?thesis
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   151
    by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   152
qed
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   153
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   154
lemma shiftr_def:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   155
  \<open>w >> n = (shiftr1 ^^ n) w\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   156
proof -
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   157
  have \<open>shiftr1 ^^ n = (drop_bit n :: 'a word \<Rightarrow> 'a word)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   158
    apply (induction n)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   159
    apply simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   160
    apply (simp only: shiftr1_eq_div_2 [abs_def] drop_bit_eq_div [abs_def] funpow_Suc_right)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   161
    apply (use div_exp_eq [of _ 1, where ?'a = \<open>'a word\<close>] in simp)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   162
    done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   163
  then show ?thesis
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   164
    by (simp add: shiftr_eq_drop_bit)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   165
qed
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   166
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   167
lemma bit_shiftl_word_iff:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   168
  \<open>bit (w << m) n \<longleftrightarrow> m \<le> n \<and> n < LENGTH('a) \<and> bit w (n - m)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   169
  for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   170
  by (simp add: shiftl_word_eq bit_push_bit_iff exp_eq_zero_iff not_le)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   171
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   172
lemma bit_shiftr_word_iff:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   173
  \<open>bit (w >> m) n \<longleftrightarrow> bit w (m + n)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   174
  for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   175
  by (simp add: shiftr_word_eq bit_drop_bit_eq)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   176
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   177
lift_definition sshiftr :: \<open>'a::len word \<Rightarrow> nat \<Rightarrow> 'a word\<close>  (infixl \<open>>>>\<close> 55)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   178
  is \<open>\<lambda>k n. take_bit LENGTH('a) (drop_bit n (signed_take_bit (LENGTH('a) - Suc 0) k))\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   179
  by (simp flip: signed_take_bit_decr_length_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   180
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   181
lemma sshiftr_eq [code]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   182
  \<open>w >>> n = signed_drop_bit n w\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   183
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   184
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   185
lemma sshiftr_eq_funpow_sshiftr1:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   186
  \<open>w >>> n = (sshiftr1 ^^ n) w\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   187
  apply (rule sym)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   188
  apply (simp add: sshiftr1_eq_signed_drop_bit_Suc_0 sshiftr_eq)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   189
  apply (induction n)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   190
   apply simp_all
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   191
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   192
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   193
lemma uint_sshiftr_eq:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   194
  \<open>uint (w >>> n) = take_bit LENGTH('a) (sint w div 2 ^  n)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   195
  for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   196
  by transfer (simp flip: drop_bit_eq_div)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   197
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   198
lemma sshift1_code [code]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   199
  \<open>sshiftr1 w = signed_drop_bit 1 w\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   200
  by transfer (simp add: drop_bit_Suc)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   201
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   202
lemma sshiftr_0 [simp]: "0 >>> n = 0"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   203
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   204
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   205
lemma sshiftr_n1 [simp]: "-1 >>> n = -1"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   206
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   207
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   208
lemma bit_sshiftr_word_iff:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   209
  \<open>bit (w >>> m) n \<longleftrightarrow> bit w (if LENGTH('a) - m \<le> n \<and> n < LENGTH('a) then LENGTH('a) - 1 else (m + n))\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   210
  for w :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   211
  apply transfer
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   212
  apply (auto simp add: bit_take_bit_iff bit_drop_bit_eq bit_signed_take_bit_iff min_def not_le simp flip: bit_Suc)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   213
  using le_less_Suc_eq apply fastforce
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   214
  using le_less_Suc_eq apply fastforce
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   215
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   216
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   217
lemma nth_sshiftr :
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   218
  "(w >>> m) !! n =
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   219
    (n < size w \<and> (if n + m \<ge> size w then w !! (size w - 1) else w !! (n + m)))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   220
  apply transfer
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   221
  apply (auto simp add: bit_take_bit_iff bit_drop_bit_eq bit_signed_take_bit_iff min_def not_le ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   222
  using le_less_Suc_eq apply fastforce
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   223
  using le_less_Suc_eq apply fastforce
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   224
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   225
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   226
lemma sshiftr_numeral [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   227
  \<open>(numeral k >>> numeral n :: 'a::len word) =
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   228
    word_of_int (drop_bit (numeral n) (signed_take_bit (LENGTH('a) - 1) (numeral k)))\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   229
  apply (rule word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   230
  apply (cases \<open>LENGTH('a)\<close>)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   231
   apply (simp_all add: word_size bit_drop_bit_eq nth_sshiftr bit_signed_take_bit_iff min_def not_le not_less less_Suc_eq_le ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   232
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   233
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   234
lemma revcast_down_us [OF refl]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   235
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >>> n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   236
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   237
  apply (simp add: source_size_def target_size_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   238
  apply (rule bit_word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   239
  apply (simp add: bit_revcast_iff bit_ucast_iff bit_sshiftr_word_iff ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   240
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   241
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   242
lemma revcast_down_ss [OF refl]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   243
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >>> n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   244
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   245
  apply (simp add: source_size_def target_size_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   246
  apply (rule bit_word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   247
  apply (simp add: bit_revcast_iff bit_word_scast_iff bit_sshiftr_word_iff ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   248
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   249
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   250
lemma sshiftr_div_2n: "sint (w >>> n) = sint w div 2 ^ n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   251
  using sint_signed_drop_bit_eq [of n w]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   252
  by (simp add: drop_bit_eq_div sshiftr_eq) 
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   253
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   254
lemmas lsb0 = len_gt_0 [THEN word_ops_nth_size [unfolded word_size]]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   255
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   256
lemma nth_sint:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   257
  fixes w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   258
  defines "l \<equiv> LENGTH('a)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   259
  shows "bit (sint w) n = (if n < l - 1 then w !! n else w !! (l - 1))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   260
  unfolding sint_uint l_def
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   261
  by (auto simp: bit_signed_take_bit_iff word_test_bit_def not_less min_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   262
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   263
lemma test_bit_2p: "(word_of_int (2 ^ n)::'a::len word) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   264
  by transfer (auto simp add: bit_exp_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   265
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   266
lemma nth_w2p: "((2::'a::len word) ^ n) !! m \<longleftrightarrow> m = n \<and> m < LENGTH('a::len)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   267
  by transfer (auto simp add: bit_exp_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   268
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   269
lemma bang_is_le: "x !! m \<Longrightarrow> 2 ^ m \<le> x"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   270
  for x :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   271
  apply (rule xtrans(3))
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   272
   apply (rule_tac [2] y = "x" in le_word_or2)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   273
  apply (rule word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   274
  apply (auto simp add: word_ao_nth nth_w2p word_size)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   275
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   276
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   277
lemma mask_eq:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   278
  \<open>mask n = (1 << n) - (1 :: 'a::len word)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   279
  by transfer (simp add: mask_eq_exp_minus_1 push_bit_of_1) 
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   280
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   281
lemma nth_ucast: "(ucast w::'a::len word) !! n = (w !! n \<and> n < LENGTH('a))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   282
  by transfer (simp add: bit_take_bit_iff ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   283
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   284
lemma shiftl_0 [simp]: "(0::'a::len word) << n = 0"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   285
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   286
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   287
lemma shiftr_0 [simp]: "(0::'a::len word) >> n = 0"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   288
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   289
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   290
lemma nth_shiftl1: "shiftl1 w !! n \<longleftrightarrow> n < size w \<and> n > 0 \<and> w !! (n - 1)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   291
  by transfer (auto simp add: bit_double_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   292
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   293
lemma nth_shiftl': "(w << m) !! n \<longleftrightarrow> n < size w \<and> n >= m \<and> w !! (n - m)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   294
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   295
  by transfer (auto simp add: bit_push_bit_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   296
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   297
lemmas nth_shiftl = nth_shiftl' [unfolded word_size]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   298
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   299
lemma nth_shiftr1: "shiftr1 w !! n = w !! Suc n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   300
  by transfer (auto simp add: bit_take_bit_iff simp flip: bit_Suc)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   301
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   302
lemma nth_shiftr: "(w >> m) !! n = w !! (n + m)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   303
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   304
  apply (unfold shiftr_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   305
  apply (induct "m" arbitrary: n)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   306
   apply (auto simp add: nth_shiftr1)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   307
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   308
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   309
lemma nth_sshiftr1: "sshiftr1 w !! n = (if n = size w - 1 then w !! n else w !! Suc n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   310
  apply transfer
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   311
  apply (auto simp add: bit_take_bit_iff bit_signed_take_bit_iff min_def simp flip: bit_Suc)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   312
  using le_less_Suc_eq apply fastforce
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   313
  using le_less_Suc_eq apply fastforce
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   314
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   315
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   316
lemma shiftr_div_2n: "uint (shiftr w n) = uint w div 2 ^ n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   317
  by (fact uint_shiftr_eq)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   318
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   319
lemma shiftl_rev: "shiftl w n = word_reverse (shiftr (word_reverse w) n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   320
  by (induct n) (auto simp add: shiftl_def shiftr_def shiftl1_rev)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   321
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   322
lemma rev_shiftl: "word_reverse w << n = word_reverse (w >> n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   323
  by (simp add: shiftl_rev)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   324
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   325
lemma shiftr_rev: "w >> n = word_reverse (word_reverse w << n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   326
  by (simp add: rev_shiftl)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   327
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   328
lemma rev_shiftr: "word_reverse w >> n = word_reverse (w << n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   329
  by (simp add: shiftr_rev)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   330
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   331
lemma shiftl_numeral [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   332
  \<open>numeral k << numeral l = (push_bit (numeral l) (numeral k) :: 'a::len word)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   333
  by (fact shiftl_word_eq)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   334
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   335
lemma shiftl_zero_size: "size x \<le> n \<Longrightarrow> x << n = 0"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   336
  for x :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   337
  apply transfer
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   338
  apply (simp add: take_bit_push_bit)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   339
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   340
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   341
lemma shiftl_t2n: "shiftl w n = 2 ^ n * w"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   342
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   343
  by (induct n) (auto simp: shiftl_def shiftl1_2t)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   344
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   345
lemma shiftr_numeral [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   346
  \<open>(numeral k >> numeral n :: 'a::len word) = drop_bit (numeral n) (numeral k)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   347
  by (fact shiftr_word_eq)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   348
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   349
lemma nth_mask [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   350
  \<open>(mask n :: 'a::len word) !! i \<longleftrightarrow> i < n \<and> i < size (mask n :: 'a word)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   351
  by (auto simp add: test_bit_word_eq word_size Word.bit_mask_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   352
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   353
lemma slice_shiftr: "slice n w = ucast (w >> n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   354
  apply (rule bit_word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   355
  apply (cases \<open>n \<le> LENGTH('b)\<close>)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   356
   apply (auto simp add: bit_slice_iff bit_ucast_iff bit_shiftr_word_iff ac_simps
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   357
    dest: bit_imp_le_length)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   358
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   359
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   360
lemma nth_slice: "(slice n w :: 'a::len word) !! m = (w !! (m + n) \<and> m < LENGTH('a))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   361
  by (simp add: slice_shiftr nth_ucast nth_shiftr)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   362
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   363
lemma revcast_down_uu [OF refl]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   364
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = ucast (w >> n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   365
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   366
  apply (simp add: source_size_def target_size_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   367
  apply (rule bit_word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   368
  apply (simp add: bit_revcast_iff bit_ucast_iff bit_shiftr_word_iff ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   369
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   370
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   371
lemma revcast_down_su [OF refl]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   372
  "rc = revcast \<Longrightarrow> source_size rc = target_size rc + n \<Longrightarrow> rc w = scast (w >> n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   373
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   374
  apply (simp add: source_size_def target_size_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   375
  apply (rule bit_word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   376
  apply (simp add: bit_revcast_iff bit_word_scast_iff bit_shiftr_word_iff ac_simps)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   377
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   378
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   379
lemma cast_down_rev [OF refl]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   380
  "uc = ucast \<Longrightarrow> source_size uc = target_size uc + n \<Longrightarrow> uc w = revcast (w << n)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   381
  for w :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   382
  apply (simp add: source_size_def target_size_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   383
  apply (rule bit_word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   384
  apply (simp add: bit_revcast_iff bit_word_ucast_iff bit_shiftl_word_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   385
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   386
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   387
lemma revcast_up [OF refl]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   388
  "rc = revcast \<Longrightarrow> source_size rc + n = target_size rc \<Longrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   389
    rc w = (ucast w :: 'a::len word) << n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   390
  apply (simp add: source_size_def target_size_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   391
  apply (rule bit_word_eqI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   392
  apply (simp add: bit_revcast_iff bit_word_ucast_iff bit_shiftl_word_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   393
  apply auto
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   394
  apply (metis add.commute add_diff_cancel_right)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   395
  apply (metis diff_add_inverse2 diff_diff_add)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   396
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   397
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   398
lemmas rc1 = revcast_up [THEN
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   399
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   400
lemmas rc2 = revcast_down_uu [THEN
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   401
  revcast_rev_ucast [symmetric, THEN trans, THEN word_rev_gal, symmetric]]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   402
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   403
lemmas ucast_up =
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   404
  rc1 [simplified rev_shiftr [symmetric] revcast_ucast [symmetric]]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   405
lemmas ucast_down =
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   406
  rc2 [simplified rev_shiftr revcast_ucast [symmetric]]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   407
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   408
\<comment> \<open>problem posed by TPHOLs referee:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   409
      criterion for overflow of addition of signed integers\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   410
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   411
lemma sofl_test:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   412
  \<open>sint x + sint y = sint (x + y) \<longleftrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   413
    (x + y XOR x) AND (x + y XOR y) >> (size x - 1) = 0\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   414
  for x y :: \<open>'a::len word\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   415
proof -
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   416
  obtain n where n: \<open>LENGTH('a) = Suc n\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   417
    by (cases \<open>LENGTH('a)\<close>) simp_all
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   418
  have *: \<open>sint x + sint y + 2 ^ Suc n > signed_take_bit n (sint x + sint y) \<Longrightarrow> sint x + sint y \<ge> - (2 ^ n)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   419
    \<open>signed_take_bit n (sint x + sint y) > sint x + sint y - 2 ^ Suc n \<Longrightarrow> 2 ^ n > sint x + sint y\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   420
    using signed_take_bit_int_greater_eq [of \<open>sint x + sint y\<close> n] signed_take_bit_int_less_eq [of n \<open>sint x + sint y\<close>]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   421
    by (auto intro: ccontr)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   422
  have \<open>sint x + sint y = sint (x + y) \<longleftrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   423
    (sint (x + y) < 0 \<longleftrightarrow> sint x < 0) \<or>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   424
    (sint (x + y) < 0 \<longleftrightarrow> sint y < 0)\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   425
    using sint_less [of x] sint_greater_eq [of x] sint_less [of y] sint_greater_eq [of y]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   426
    signed_take_bit_int_eq_self [of \<open>LENGTH('a) - 1\<close> \<open>sint x + sint y\<close>]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   427
    apply (auto simp add: not_less)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   428
       apply (unfold sint_word_ariths)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   429
       apply (subst signed_take_bit_int_eq_self)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   430
         prefer 4
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   431
       apply (subst signed_take_bit_int_eq_self)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   432
         prefer 7
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   433
       apply (subst signed_take_bit_int_eq_self)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   434
         prefer 10
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   435
             apply (subst signed_take_bit_int_eq_self)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   436
       apply (auto simp add: signed_take_bit_int_eq_self signed_take_bit_eq_take_bit_minus take_bit_Suc_from_most n not_less intro!: *)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   437
    done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   438
  then show ?thesis
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   439
    apply (simp only: One_nat_def word_size shiftr_word_eq drop_bit_eq_zero_iff_not_bit_last bit_and_iff bit_xor_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   440
    apply (simp add: bit_last_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   441
    done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   442
qed
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   443
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   444
lemma shiftr_zero_size: "size x \<le> n \<Longrightarrow> x >> n = 0"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   445
  for x :: "'a :: len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   446
  by (rule word_eqI) (auto simp add: nth_shiftr dest: test_bit_size)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   447
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   448
lemma test_bit_cat [OF refl]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   449
  "wc = word_cat a b \<Longrightarrow> wc !! n = (n < size wc \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   450
    (if n < size b then b !! n else a !! (n - size b)))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   451
  apply (simp add: word_size not_less; transfer)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   452
       apply (auto simp add: bit_concat_bit_iff bit_take_bit_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   453
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   454
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   455
\<comment> \<open>keep quantifiers for use in simplification\<close>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   456
lemma test_bit_split':
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   457
  "word_split c = (a, b) \<longrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   458
    (\<forall>n m.
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   459
      b !! n = (n < size b \<and> c !! n) \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   460
      a !! m = (m < size a \<and> c !! (m + size b)))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   461
  by (auto simp add: word_split_bin' test_bit_bin bit_unsigned_iff word_size
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   462
    bit_drop_bit_eq ac_simps exp_eq_zero_iff
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   463
    dest: bit_imp_le_length)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   464
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   465
lemma test_bit_split:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   466
  "word_split c = (a, b) \<Longrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   467
    (\<forall>n::nat. b !! n \<longleftrightarrow> n < size b \<and> c !! n) \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   468
    (\<forall>m::nat. a !! m \<longleftrightarrow> m < size a \<and> c !! (m + size b))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   469
  by (simp add: test_bit_split')
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   470
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   471
lemma test_bit_split_eq:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   472
  "word_split c = (a, b) \<longleftrightarrow>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   473
    ((\<forall>n::nat. b !! n = (n < size b \<and> c !! n)) \<and>
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   474
     (\<forall>m::nat. a !! m = (m < size a \<and> c !! (m + size b))))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   475
  apply (rule_tac iffI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   476
   apply (rule_tac conjI)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   477
    apply (erule test_bit_split [THEN conjunct1])
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   478
   apply (erule test_bit_split [THEN conjunct2])
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   479
  apply (case_tac "word_split c")
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   480
  apply (frule test_bit_split)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   481
  apply (erule trans)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   482
  apply (fastforce intro!: word_eqI simp add: word_size)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   483
  done
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   484
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   485
lemma test_bit_rcat:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   486
  "sw = size (hd wl) \<Longrightarrow> rc = word_rcat wl \<Longrightarrow> rc !! n =
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   487
    (n < size rc \<and> n div sw < size wl \<and> (rev wl) ! (n div sw) !! (n mod sw))"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   488
  for wl :: "'a::len word list"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   489
  by (simp add: word_size word_rcat_def foldl_map rev_map bit_horner_sum_uint_exp_iff)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   490
    (simp add: test_bit_eq_bit)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   491
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   492
lemmas test_bit_cong = arg_cong [where f = "test_bit", THEN fun_cong]
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   493
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   494
lemma max_test_bit: "(max_word::'a::len word) !! n \<longleftrightarrow> n < LENGTH('a)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   495
  by (fact nth_minus1)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   496
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   497
lemma shiftr_x_0 [iff]: "x >> 0 = x"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   498
  for x :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   499
  by transfer simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   500
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   501
lemma shiftl_x_0 [simp]: "x << 0 = x"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   502
  for x :: "'a::len word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   503
  by (simp add: shiftl_t2n)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   504
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   505
lemma shiftl_1 [simp]: "(1::'a::len word) << n = 2^n"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   506
  by (simp add: shiftl_t2n)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   507
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   508
lemma shiftr_1[simp]: "(1::'a::len word) >> n = (if n = 0 then 1 else 0)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   509
  by (induct n) (auto simp: shiftr_def)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   510
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   511
lemma map_nth_0 [simp]: "map ((!!) (0::'a::len word)) xs = replicate (length xs) False"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   512
  by (induct xs) auto
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   513
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   514
lemma word_and_1:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   515
  "n AND 1 = (if n !! 0 then 1 else 0)" for n :: "_ word"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   516
  by (rule bit_word_eqI) (auto simp add: bit_and_iff test_bit_eq_bit bit_1_iff intro: gr0I)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   517
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   518
lemma test_bit_1' [simp]:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   519
  "(1 :: 'a :: len word) !! n \<longleftrightarrow> 0 < LENGTH('a) \<and> n = 0"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   520
  by simp
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   521
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   522
lemma shiftl0:
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   523
  "x << 0 = (x :: 'a :: len word)"
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   524
  by (fact shiftl_x_0)
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   525
c89d8e8bd8c7 factored out theory Traditional_Syntax
haftmann
parents: 72000
diff changeset
   526
end