src/HOL/Word/Bits_Int.thy
author haftmann
Tue, 04 Aug 2020 09:33:05 +0000
changeset 72082 41393ecb57ac
parent 72081 e4d42f5766dc
child 72088 a36db1c8238e
permissions -rw-r--r--
uniform mask operation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     1
(*  Title:      HOL/Word/Bits_Int.thy
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     2
    Author:     Jeremy Dawson and Gerwin Klein, NICTA
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     3
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
     5
section \<open>Bitwise Operations on integers\<close>
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     6
54854
3324a0078636 prefer "Bits" as theory name for abstract bit operations, similar to "Orderings", "Lattices", "Groups" etc.
haftmann
parents: 54848
diff changeset
     7
theory Bits_Int
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
     8
  imports
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
     9
    "HOL-Library.Bit_Operations"
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
    10
    Traditional_Syntax
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    11
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    12
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
    13
subsection \<open>Generic auxiliary\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
    14
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
    15
lemma int_mod_ge: "a < n \<Longrightarrow> 0 < n \<Longrightarrow> a \<le> a mod n"
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
    16
  for a n :: int
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
    17
  by (metis dual_order.trans le_cases mod_pos_pos_trivial pos_mod_conj)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
    18
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
    19
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    20
subsection \<open>Implicit bit representation of \<^typ>\<open>int\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    21
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    22
abbreviation (input) bin_last :: "int \<Rightarrow> bool"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    23
  where "bin_last \<equiv> odd"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    24
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    25
lemma bin_last_def:
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    26
  "bin_last w \<longleftrightarrow> w mod 2 = 1"
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
    27
  by (fact odd_iff_mod_2_eq_one)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    28
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    29
abbreviation (input) bin_rest :: "int \<Rightarrow> int"
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    30
  where "bin_rest w \<equiv> w div 2"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    31
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    32
lemma bin_last_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    33
  "\<not> bin_last 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    34
  "bin_last 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    35
  "bin_last (- 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    36
  "bin_last Numeral1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    37
  "\<not> bin_last (numeral (Num.Bit0 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    38
  "bin_last (numeral (Num.Bit1 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    39
  "\<not> bin_last (- numeral (Num.Bit0 w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    40
  "bin_last (- numeral (Num.Bit1 w))"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    41
  by simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    42
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    43
lemma bin_rest_numeral_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    44
  "bin_rest 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    45
  "bin_rest 1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    46
  "bin_rest (- 1) = - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    47
  "bin_rest Numeral1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    48
  "bin_rest (numeral (Num.Bit0 w)) = numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    49
  "bin_rest (numeral (Num.Bit1 w)) = numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    50
  "bin_rest (- numeral (Num.Bit0 w)) = - numeral w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    51
  "bin_rest (- numeral (Num.Bit1 w)) = - numeral (w + Num.One)"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    52
  by simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    53
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    54
lemma bin_rl_eqI: "\<lbrakk>bin_rest x = bin_rest y; bin_last x = bin_last y\<rbrakk> \<Longrightarrow> x = y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
    55
  by (auto elim: oddE)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    56
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    57
lemma [simp]: 
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    58
  shows bin_rest_lt0: "bin_rest i < 0 \<longleftrightarrow> i < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    59
  and  bin_rest_ge_0: "bin_rest i \<ge> 0 \<longleftrightarrow> i \<ge> 0"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    60
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    61
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    62
lemma bin_rest_gt_0 [simp]: "bin_rest x > 0 \<longleftrightarrow> x > 1"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
    63
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    64
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    65
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    66
subsection \<open>Bit projection\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    67
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    68
abbreviation (input) bin_nth :: \<open>int \<Rightarrow> nat \<Rightarrow> bool\<close>
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    69
  where \<open>bin_nth \<equiv> bit\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    70
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    71
lemma bin_nth_eq_iff: "bin_nth x = bin_nth y \<longleftrightarrow> x = y"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    72
  by (simp add: bit_eq_iff fun_eq_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    73
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    74
lemma bin_eqI:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    75
  "x = y" if "\<And>n. bin_nth x n \<longleftrightarrow> bin_nth y n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    76
  using that bin_nth_eq_iff [of x y] by (simp add: fun_eq_iff)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    77
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    78
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    79
  by (fact bit_eq_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    80
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    81
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    82
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    83
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    84
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    85
  by (cases n) (simp_all add: bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    86
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    87
lemma bin_nth_minus1 [simp]: "bin_nth (- 1) n"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    88
  by (induction n) (simp_all add: bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    89
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    90
lemma bin_nth_numeral: "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
    91
  by (simp add: numeral_eq_Suc bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    92
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    93
lemmas bin_nth_numeral_simps [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    94
  bin_nth_numeral [OF bin_rest_numeral_simps(2)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    95
  bin_nth_numeral [OF bin_rest_numeral_simps(5)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    96
  bin_nth_numeral [OF bin_rest_numeral_simps(6)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    97
  bin_nth_numeral [OF bin_rest_numeral_simps(7)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    98
  bin_nth_numeral [OF bin_rest_numeral_simps(8)]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
    99
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   100
lemmas bin_nth_simps =
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   101
  bit_0 bit_Suc bin_nth_zero bin_nth_minus1
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   102
  bin_nth_numeral_simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   103
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   104
lemma nth_2p_bin: "bin_nth (2 ^ n) m = (m = n)" \<comment> \<open>for use when simplifying with \<open>bin_nth_Bit\<close>\<close>
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   105
  by (auto simp add: bit_exp_iff)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   106
  
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   107
lemma nth_rest_power_bin: "bin_nth ((bin_rest ^^ k) w) n = bin_nth w (n + k)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   108
  apply (induct k arbitrary: n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   109
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   110
  apply clarsimp
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   111
  apply (simp only: bit_Suc [symmetric] add_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   112
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   113
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   114
lemma bin_nth_numeral_unfold:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   115
  "bin_nth (numeral (num.Bit0 x)) n \<longleftrightarrow> n > 0 \<and> bin_nth (numeral x) (n - 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   116
  "bin_nth (numeral (num.Bit1 x)) n \<longleftrightarrow> (n > 0 \<longrightarrow> bin_nth (numeral x) (n - 1))"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
   117
  by (cases n; simp)+
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   118
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   119
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   120
subsection \<open>Truncating\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   121
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   122
definition bin_sign :: "int \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   123
  where "bin_sign k = (if k \<ge> 0 then 0 else - 1)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   124
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   125
lemma bin_sign_simps [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   126
  "bin_sign 0 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   127
  "bin_sign 1 = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   128
  "bin_sign (- 1) = - 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   129
  "bin_sign (numeral k) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   130
  "bin_sign (- numeral k) = -1"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   131
  by (simp_all add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   132
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   133
lemma bin_sign_rest [simp]: "bin_sign (bin_rest w) = bin_sign w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   134
  by (simp add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   135
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   136
abbreviation (input) bintrunc :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   137
  where \<open>bintrunc \<equiv> take_bit\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   138
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   139
lemma bintrunc_mod2p: "bintrunc n w = w mod 2 ^ n"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   140
  by (fact take_bit_eq_mod)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   141
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   142
abbreviation (input) sbintrunc :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   143
  where \<open>sbintrunc \<equiv> signed_take_bit\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   144
72042
587d4681240c yet another alias
haftmann
parents: 72028
diff changeset
   145
abbreviation (input) norm_sint :: \<open>nat \<Rightarrow> int \<Rightarrow> int\<close>
587d4681240c yet another alias
haftmann
parents: 72028
diff changeset
   146
  where \<open>norm_sint n \<equiv> signed_take_bit (n - 1)\<close>
587d4681240c yet another alias
haftmann
parents: 72028
diff changeset
   147
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   148
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   149
  by (simp add: bintrunc_mod2p signed_take_bit_eq_take_bit_shift)
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   150
  
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   151
lemma sbintrunc_eq_take_bit:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   152
  \<open>sbintrunc n k = take_bit (Suc n) (k + 2 ^ n) - 2 ^ n\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   153
  by (fact signed_take_bit_eq_take_bit_shift)
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   154
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   155
lemma sign_bintr: "bin_sign (bintrunc n w) = 0"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   156
  by (simp add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   157
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   158
lemma bintrunc_n_0: "bintrunc n 0 = 0"
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   159
  by (fact take_bit_of_0)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   160
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   161
lemma sbintrunc_n_0: "sbintrunc n 0 = 0"
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   162
  by (fact signed_take_bit_of_0)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   163
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   164
lemma sbintrunc_n_minus1: "sbintrunc n (- 1) = -1"
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   165
  by (fact signed_take_bit_of_minus_1)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   166
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   167
lemma bintrunc_Suc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   168
  "bintrunc (Suc n) 1 = 1"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   169
  "bintrunc (Suc n) (- 1) = 1 + 2 * bintrunc n (- 1)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   170
  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * bintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   171
  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   172
  "bintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * bintrunc n (- numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   173
  "bintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * bintrunc n (- numeral (w + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   174
  by (simp_all add: take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   175
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   176
lemma sbintrunc_0_numeral [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   177
  "sbintrunc 0 1 = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   178
  "sbintrunc 0 (numeral (Num.Bit0 w)) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   179
  "sbintrunc 0 (numeral (Num.Bit1 w)) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   180
  "sbintrunc 0 (- numeral (Num.Bit0 w)) = 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   181
  "sbintrunc 0 (- numeral (Num.Bit1 w)) = -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   182
  by simp_all
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   183
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   184
lemma sbintrunc_Suc_numeral:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   185
  "sbintrunc (Suc n) 1 = 1"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   186
  "sbintrunc (Suc n) (numeral (Num.Bit0 w)) = 2 * sbintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   187
  "sbintrunc (Suc n) (numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   188
  "sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = 2 * sbintrunc n (- numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   189
  "sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = 1 + 2 * sbintrunc n (- numeral (w + Num.One))"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   190
  by (simp_all add: signed_take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   191
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   192
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bit bin n"
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   193
  by (simp add: bin_sign_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   194
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   195
lemma nth_bintr: "bin_nth (bintrunc m w) n \<longleftrightarrow> n < m \<and> bin_nth w n"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   196
  by (fact bit_take_bit_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   197
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   198
lemma nth_sbintr: "bin_nth (sbintrunc m w) n = (if n < m then bin_nth w n else bin_nth w m)"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   199
  by (simp add: bit_signed_take_bit_iff min_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   200
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   201
lemma bin_nth_Bit0:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   202
  "bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   203
    (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   204
  using bit_double_iff [of \<open>numeral w :: int\<close> n]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   205
  by (auto intro: exI [of _ \<open>n - 1\<close>])
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   206
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   207
lemma bin_nth_Bit1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   208
  "bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   209
    n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   210
  using even_bit_succ_iff [of \<open>2 * numeral w :: int\<close> n]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   211
    bit_double_iff [of \<open>numeral w :: int\<close> n]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   212
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   213
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   214
lemma bintrunc_bintrunc_l: "n \<le> m \<Longrightarrow> bintrunc m (bintrunc n w) = bintrunc n w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   215
  by (simp add: min.absorb2)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   216
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   217
lemma sbintrunc_sbintrunc_l: "n \<le> m \<Longrightarrow> sbintrunc m (sbintrunc n w) = sbintrunc n w"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   218
  by (simp add: min_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   219
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   220
lemma bintrunc_bintrunc_ge: "n \<le> m \<Longrightarrow> bintrunc n (bintrunc m w) = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   221
  by (rule bin_eqI) (auto simp: nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   222
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   223
lemma bintrunc_bintrunc_min [simp]: "bintrunc m (bintrunc n w) = bintrunc (min m n) w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   224
  by (rule bin_eqI) (auto simp: nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   225
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   226
lemma sbintrunc_sbintrunc_min [simp]: "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   227
  by (rule bin_eqI) (auto simp: nth_sbintr min.absorb1 min.absorb2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   228
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   229
lemmas sbintrunc_Suc_Pls =
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   230
  signed_take_bit_Suc [where k="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   231
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   232
lemmas sbintrunc_Suc_Min =
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   233
  signed_take_bit_Suc [where k="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   234
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   235
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   236
  sbintrunc_Suc_numeral
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   237
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   238
lemmas sbintrunc_Pls =
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   239
  signed_take_bit_0 [where k="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   240
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   241
lemmas sbintrunc_Min =
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   242
  signed_take_bit_0 [where k="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   243
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   244
lemmas sbintrunc_0_simps =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   245
  sbintrunc_Pls sbintrunc_Min
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   246
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   247
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   248
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   249
lemma bintrunc_minus: "0 < n \<Longrightarrow> bintrunc (Suc (n - 1)) w = bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   250
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   251
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   252
lemma sbintrunc_minus: "0 < n \<Longrightarrow> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   253
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   254
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   255
lemmas sbintrunc_minus_simps =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   256
  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   257
71984
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   258
lemma sbintrunc_BIT_I:
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   259
  \<open>0 < n \<Longrightarrow>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   260
  sbintrunc (n - 1) 0 = y \<Longrightarrow>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   261
  sbintrunc n 0 = 2 * y\<close>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   262
  by simp
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   263
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   264
lemma sbintrunc_Suc_Is:
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   265
  \<open>sbintrunc n (- 1) = y \<Longrightarrow>
10a8d943a8d8 more explicit proofs
haftmann
parents: 71957
diff changeset
   266
  sbintrunc (Suc n) (- 1) = 1 + 2 * y\<close>
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   267
  by auto
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   268
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   269
lemma sbintrunc_Suc_lem: "sbintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> sbintrunc m x = y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   270
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   271
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   272
lemmas sbintrunc_Suc_Ialts =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   273
  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   274
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   275
lemma sbintrunc_bintrunc_lt: "m > n \<Longrightarrow> sbintrunc n (bintrunc m w) = sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   276
  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   277
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   278
lemma bintrunc_sbintrunc_le: "m \<le> Suc n \<Longrightarrow> bintrunc m (sbintrunc n w) = bintrunc m w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   279
  apply (rule bin_eqI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   280
  using le_Suc_eq less_Suc_eq_le apply (auto simp: nth_sbintr nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   281
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   282
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   283
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   284
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   285
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   286
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   287
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   288
lemma bintrunc_sbintrunc' [simp]: "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   289
  by (cases n) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   290
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   291
lemma sbintrunc_bintrunc' [simp]: "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   292
  by (cases n) simp_all
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   293
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   294
lemma bin_sbin_eq_iff: "bintrunc (Suc n) x = bintrunc (Suc n) y \<longleftrightarrow> sbintrunc n x = sbintrunc n y"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   295
  apply (rule iffI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   296
   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   297
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   298
  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   299
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   300
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   301
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   302
lemma bin_sbin_eq_iff':
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   303
  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y \<longleftrightarrow> sbintrunc (n - 1) x = sbintrunc (n - 1) y"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   304
  by (cases n) (simp_all add: bin_sbin_eq_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   305
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   306
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   307
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   308
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   309
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   310
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   311
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   312
(* although bintrunc_minus_simps, if added to default simpset,
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   313
  tends to get applied where it's not wanted in developing the theories,
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   314
  we get a version for when the word length is given literally *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   315
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   316
lemmas nat_non0_gr =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   317
  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   318
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   319
lemma bintrunc_numeral:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   320
  "bintrunc (numeral k) x = of_bool (odd x) + 2 * bintrunc (pred_numeral k) (x div 2)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   321
  by (simp add: numeral_eq_Suc take_bit_Suc mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   322
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   323
lemma sbintrunc_numeral:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   324
  "sbintrunc (numeral k) x = of_bool (odd x) + 2 * sbintrunc (pred_numeral k) (x div 2)"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   325
  by (simp add: numeral_eq_Suc signed_take_bit_Suc mod2_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   326
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   327
lemma bintrunc_numeral_simps [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   328
  "bintrunc (numeral k) (numeral (Num.Bit0 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   329
    2 * bintrunc (pred_numeral k) (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   330
  "bintrunc (numeral k) (numeral (Num.Bit1 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   331
    1 + 2 * bintrunc (pred_numeral k) (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   332
  "bintrunc (numeral k) (- numeral (Num.Bit0 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   333
    2 * bintrunc (pred_numeral k) (- numeral w)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   334
  "bintrunc (numeral k) (- numeral (Num.Bit1 w)) =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   335
    1 + 2 * bintrunc (pred_numeral k) (- numeral (w + Num.One))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   336
  "bintrunc (numeral k) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   337
  by (simp_all add: bintrunc_numeral)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   338
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   339
lemma sbintrunc_numeral_simps [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   340
  "sbintrunc (numeral k) (numeral (Num.Bit0 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   341
    2 * sbintrunc (pred_numeral k) (numeral w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   342
  "sbintrunc (numeral k) (numeral (Num.Bit1 w)) =
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   343
    1 + 2 * sbintrunc (pred_numeral k) (numeral w)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   344
  "sbintrunc (numeral k) (- numeral (Num.Bit0 w)) =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   345
    2 * sbintrunc (pred_numeral k) (- numeral w)"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   346
  "sbintrunc (numeral k) (- numeral (Num.Bit1 w)) =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   347
    1 + 2 * sbintrunc (pred_numeral k) (- numeral (w + Num.One))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   348
  "sbintrunc (numeral k) 1 = 1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   349
  by (simp_all add: sbintrunc_numeral)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   350
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   351
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   352
  by (rule ext) (rule bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   353
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   354
lemma range_bintrunc: "range (bintrunc n) = {i. 0 \<le> i \<and> i < 2 ^ n}"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   355
  by (auto simp add: take_bit_eq_mod image_iff) (metis mod_pos_pos_trivial)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   356
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   357
lemma no_sbintr_alt2: "sbintrunc n = (\<lambda>w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   358
  by (rule ext) (simp add : sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   359
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   360
lemma range_sbintrunc: "range (sbintrunc n) = {i. - (2 ^ n) \<le> i \<and> i < 2 ^ n}"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   361
proof -
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   362
  have \<open>surj (\<lambda>k::int. k + 2 ^ n)\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   363
    by (rule surjI [of _ \<open>(\<lambda>k. k - 2 ^ n)\<close>]) simp
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   364
  moreover have \<open>sbintrunc n = ((\<lambda>k. k - 2 ^ n) \<circ> take_bit (Suc n) \<circ> (\<lambda>k. k + 2 ^ n))\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   365
    by (simp add: sbintrunc_eq_take_bit fun_eq_iff)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   366
  ultimately show ?thesis
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   367
    apply (simp only: fun.set_map range_bintrunc)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   368
    apply (auto simp add: image_iff)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   369
    apply presburger
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   370
    done
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   371
qed
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   372
  
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   373
lemma sbintrunc_inc:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   374
  \<open>k + 2 ^ Suc n \<le> sbintrunc n k\<close> if \<open>k < - (2 ^ n)\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   375
  using that by (fact signed_take_bit_greater_eq)
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   376
  
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   377
lemma sbintrunc_dec:
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   378
  \<open>sbintrunc n k \<le> k - 2 ^ (Suc n)\<close> if \<open>k \<ge> 2 ^ n\<close>
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   379
  using that by (fact signed_take_bit_less_eq)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   380
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   381
lemma bintr_ge0: "0 \<le> bintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   382
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   383
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   384
lemma bintr_lt2p: "bintrunc n w < 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   385
  by (simp add: bintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   386
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   387
lemma bintr_Min: "bintrunc n (- 1) = 2 ^ n - 1"
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   388
  by (simp add: stable_imp_take_bit_eq)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   389
  
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   390
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   391
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   392
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   393
lemma sbintr_lt: "sbintrunc n w < 2 ^ n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   394
  by (simp add: sbintrunc_mod2p)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   395
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   396
lemma sign_Pls_ge_0: "bin_sign bin = 0 \<longleftrightarrow> bin \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   397
  for bin :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   398
  by (simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   399
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   400
lemma sign_Min_lt_0: "bin_sign bin = -1 \<longleftrightarrow> bin < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   401
  for bin :: int
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   402
  by (simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   403
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   404
lemma bin_rest_trunc: "bin_rest (bintrunc n bin) = bintrunc (n - 1) (bin_rest bin)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   405
  by (simp add: take_bit_rec [of n bin])
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   406
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   407
lemma bin_rest_power_trunc:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   408
  "(bin_rest ^^ k) (bintrunc n bin) = bintrunc (n - k) ((bin_rest ^^ k) bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   409
  by (induct k) (auto simp: bin_rest_trunc)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   410
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   411
lemma bin_rest_trunc_i: "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   412
  by (auto simp add: take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   413
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   414
lemma bin_rest_strunc: "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   415
  by (simp add: signed_take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   416
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   417
lemma bintrunc_rest [simp]: "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   418
  by (induct n arbitrary: bin) (simp_all add: take_bit_Suc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   419
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   420
lemma sbintrunc_rest [simp]: "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
72010
a851ce626b78 signed_take_bit
haftmann
parents: 72000
diff changeset
   421
  by (induct n arbitrary: bin) (simp_all add: signed_take_bit_Suc mod2_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   422
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   423
lemma bintrunc_rest': "bintrunc n \<circ> bin_rest \<circ> bintrunc n = bin_rest \<circ> bintrunc n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   424
  by (rule ext) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   425
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   426
lemma sbintrunc_rest': "sbintrunc n \<circ> bin_rest \<circ> sbintrunc n = bin_rest \<circ> sbintrunc n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   427
  by (rule ext) auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   428
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   429
lemma rco_lem: "f \<circ> g \<circ> f = g \<circ> f \<Longrightarrow> f \<circ> (g \<circ> f) ^^ n = g ^^ n \<circ> f"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   430
  apply (rule ext)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   431
  apply (induct_tac n)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   432
   apply (simp_all (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   433
  apply (drule fun_cong)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   434
  apply (unfold o_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   435
  apply (erule trans)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   436
  apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   437
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   438
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   439
lemmas rco_bintr = bintrunc_rest'
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   440
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   441
lemmas rco_sbintr = sbintrunc_rest'
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   442
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   443
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   444
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   445
subsection \<open>Splitting and concatenation\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   446
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   447
definition bin_split :: \<open>nat \<Rightarrow> int \<Rightarrow> int \<times> int\<close>
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   448
  where [simp]: \<open>bin_split n k = (drop_bit n k, take_bit n k)\<close>
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   449
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   450
lemma [code]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   451
  "bin_split (Suc n) w = (let (w1, w2) = bin_split n (w div 2) in (w1, of_bool (odd w) + 2 * w2))"
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   452
  "bin_split 0 w = (w, 0)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   453
  by (simp_all add: drop_bit_Suc take_bit_Suc mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   454
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   455
abbreviation (input) bin_cat :: \<open>int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int\<close>
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   456
  where \<open>bin_cat k n l \<equiv> concat_bit n l k\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   457
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   458
lemma bin_cat_eq_push_bit_add_take_bit:
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   459
  \<open>bin_cat k n l = push_bit n k + take_bit n l\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   460
  by (simp add: concat_bit_eq)
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   461
  
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   462
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   463
proof -
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   464
  have \<open>0 \<le> x\<close> if \<open>0 \<le> x * 2 ^ n + y mod 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   465
  proof -
71997
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   466
    have \<open>y mod 2 ^ n < 2 ^ n\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   467
      using pos_mod_bound [of \<open>2 ^ n\<close> y] by simp
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   468
    then have \<open>\<not> y mod 2 ^ n \<ge> 2 ^ n\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   469
      by (simp add: less_le)
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   470
    with that have \<open>x \<noteq> - 1\<close>
4a013c92a091 factored out auxiliary theory
haftmann
parents: 71991
diff changeset
   471
      by auto
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   472
    have *: \<open>- 1 \<le> (- (y mod 2 ^ n)) div 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   473
      by (simp add: zdiv_zminus1_eq_if)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   474
    from that have \<open>- (y mod 2 ^ n) \<le> x * 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   475
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   476
    then have \<open>(- (y mod 2 ^ n)) div 2 ^ n \<le> (x * 2 ^ n) div 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   477
      using zdiv_mono1 zero_less_numeral zero_less_power by blast
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   478
    with * have \<open>- 1 \<le> x * 2 ^ n div 2 ^ n\<close> by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   479
    with \<open>x \<noteq> - 1\<close> show ?thesis
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   480
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   481
  qed
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   482
  then show ?thesis
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   483
    by (simp add: bin_sign_def not_le not_less bin_cat_eq_push_bit_add_take_bit push_bit_eq_mult take_bit_eq_mod)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   484
qed
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   485
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   486
lemma bin_cat_assoc: "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)"
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   487
  by (fact concat_bit_assoc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   488
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   489
lemma bin_cat_assoc_sym: "bin_cat x m (bin_cat y n z) = bin_cat (bin_cat x (m - n) y) (min m n) z"
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   490
  by (fact concat_bit_assoc_sym)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   491
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   492
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   493
  where "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   494
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   495
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   496
  where "bin_rsplit_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   497
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   498
     else
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   499
      let (a, b) = bin_split n c
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   500
      in bin_rsplit_aux n (m - n) a (b # bs))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   501
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   502
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   503
  where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   504
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   505
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   506
  where "bin_rsplitl_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   507
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   508
     else
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   509
      let (a, b) = bin_split (min m n) c
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   510
      in bin_rsplitl_aux n (m - n) a (b # bs))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   511
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   512
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   513
  where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   514
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   515
declare bin_rsplit_aux.simps [simp del]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   516
declare bin_rsplitl_aux.simps [simp del]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   517
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   518
lemma bin_nth_cat:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   519
  "bin_nth (bin_cat x k y) n =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   520
    (if n < k then bin_nth y n else bin_nth x (n - k))"
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   521
  by (simp add: bit_concat_bit_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   522
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   523
lemma bin_nth_drop_bit_iff:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   524
  \<open>bin_nth (drop_bit n c) k \<longleftrightarrow> bin_nth c (n + k)\<close>
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   525
  by (simp add: bit_drop_bit_eq)
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   526
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   527
lemma bin_nth_take_bit_iff:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   528
  \<open>bin_nth (take_bit n c) k \<longleftrightarrow> k < n \<and> bin_nth c k\<close>
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   529
  by (fact bit_take_bit_iff)
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   530
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   531
lemma bin_nth_split:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   532
  "bin_split n c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   533
    (\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   534
    (\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   535
  by (auto simp add: bin_nth_drop_bit_iff bin_nth_take_bit_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   536
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   537
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   538
  by (simp add: bin_cat_eq_push_bit_add_take_bit)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   539
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   540
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   541
  by (metis bin_cat_assoc bin_cat_zero)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   542
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   543
lemma bintr_cat: "bintrunc m (bin_cat a n b) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   544
    bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)"
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   545
  
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   546
  by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   547
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   548
lemma bintr_cat_same [simp]: "bintrunc n (bin_cat a n b) = bintrunc n b"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   549
  by (auto simp add : bintr_cat)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   550
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   551
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   552
  by (simp add: bin_cat_eq_push_bit_add_take_bit)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   553
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   554
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   555
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   556
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   557
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   558
  by (auto simp add: bin_cat_eq_push_bit_add_take_bit bits_ident)
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   559
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   560
lemma drop_bit_bin_cat_eq:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   561
  \<open>drop_bit n (bin_cat v n w) = v\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   562
  by (rule bit_eqI) (simp add: bit_drop_bit_eq bit_concat_bit_iff)
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   563
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   564
lemma take_bit_bin_cat_eq:
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   565
  \<open>take_bit n (bin_cat v n w) = take_bit n w\<close>
72028
08f1e4cb735f concatentation of bit values
haftmann
parents: 72010
diff changeset
   566
  by (rule bit_eqI) (simp add: bit_concat_bit_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   567
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   568
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   569
  by (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   570
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   571
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   572
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   573
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   574
lemma bin_split_minus1 [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   575
  "bin_split n (- 1) = (- 1, bintrunc n (- 1))"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   576
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   577
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   578
lemma bin_split_trunc:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   579
  "bin_split (min m n) c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   580
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   581
  apply (induct n arbitrary: m b c, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   582
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   583
  apply (case_tac m)
71946
4d4079159be7 replaced mere alias by abbreviation
haftmann
parents: 71945
diff changeset
   584
   apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   585
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   586
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   587
lemma bin_split_trunc1:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   588
  "bin_split n c = (a, b) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   589
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   590
  apply (induct n arbitrary: m b c, clarsimp)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   591
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   592
  apply (case_tac m)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   593
   apply (auto simp: Let_def drop_bit_Suc take_bit_Suc mod_2_eq_odd split: prod.split_asm)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   594
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   595
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   596
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   597
  by (simp add: bin_cat_eq_push_bit_add_take_bit push_bit_eq_mult)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   598
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   599
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)"
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   600
  by (simp add: drop_bit_eq_div take_bit_eq_mod)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   601
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   602
lemmas bin_rsplit_aux_simps = bin_rsplit_aux.simps bin_rsplitl_aux.simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   603
lemmas rsplit_aux_simps = bin_rsplit_aux_simps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   604
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   605
lemmas th_if_simp1 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct1, THEN mp] for l
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   606
lemmas th_if_simp2 = if_split [where P = "(=) l", THEN iffD1, THEN conjunct2, THEN mp] for l
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   607
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   608
lemmas rsplit_aux_simp1s = rsplit_aux_simps [THEN th_if_simp1]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   609
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   610
lemmas rsplit_aux_simp2ls = rsplit_aux_simps [THEN th_if_simp2]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   611
\<comment> \<open>these safe to \<open>[simp add]\<close> as require calculating \<open>m - n\<close>\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   612
lemmas bin_rsplit_aux_simp2s [simp] = rsplit_aux_simp2ls [unfolded Let_def]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   613
lemmas rbscl = bin_rsplit_aux_simp2s (2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   614
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   615
lemmas rsplit_aux_0_simps [simp] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   616
  rsplit_aux_simp1s [OF disjI1] rsplit_aux_simp1s [OF disjI2]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   617
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   618
lemma bin_rsplit_aux_append: "bin_rsplit_aux n m c (bs @ cs) = bin_rsplit_aux n m c bs @ cs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   619
  apply (induct n m c bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   620
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   621
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   622
  apply (clarsimp split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   623
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   624
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   625
lemma bin_rsplitl_aux_append: "bin_rsplitl_aux n m c (bs @ cs) = bin_rsplitl_aux n m c bs @ cs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   626
  apply (induct n m c bs rule: bin_rsplitl_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   627
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   628
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   629
  apply (clarsimp split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   630
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   631
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   632
lemmas rsplit_aux_apps [where bs = "[]"] =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   633
  bin_rsplit_aux_append bin_rsplitl_aux_append
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   634
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   635
lemmas rsplit_def_auxs = bin_rsplit_def bin_rsplitl_def
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   636
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   637
lemmas rsplit_aux_alts = rsplit_aux_apps
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   638
  [unfolded append_Nil rsplit_def_auxs [symmetric]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   639
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   640
lemma bin_split_minus: "0 < n \<Longrightarrow> bin_split (Suc (n - 1)) w = bin_split n w"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   641
  by auto
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   642
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   643
lemma bin_split_pred_simp [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   644
  "(0::nat) < numeral bin \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   645
    bin_split (numeral bin) w =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   646
      (let (w1, w2) = bin_split (numeral bin - 1) (bin_rest w)
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   647
       in (w1, of_bool (odd w) + 2 * w2))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   648
  by (simp add: take_bit_rec drop_bit_rec mod_2_eq_odd)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   649
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   650
lemma bin_rsplit_aux_simp_alt:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   651
  "bin_rsplit_aux n m c bs =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   652
    (if m = 0 \<or> n = 0 then bs
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   653
     else let (a, b) = bin_split n c in bin_rsplit n (m - n, a) @ b # bs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   654
  apply (simp add: bin_rsplit_aux.simps [of n m c bs])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   655
  apply (subst rsplit_aux_alts)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   656
  apply (simp add: bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   657
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   658
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   659
lemmas bin_rsplit_simp_alt =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   660
  trans [OF bin_rsplit_def bin_rsplit_aux_simp_alt]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   661
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   662
lemmas bthrs = bin_rsplit_simp_alt [THEN [2] trans]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   663
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   664
lemma bin_rsplit_size_sign' [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   665
  "n > 0 \<Longrightarrow> rev sw = bin_rsplit n (nw, w) \<Longrightarrow> \<forall>v\<in>set sw. bintrunc n v = v"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   666
  apply (induct sw arbitrary: nw w)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   667
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   668
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   669
  apply (drule bthrs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   670
  apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   671
  apply clarify
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   672
  apply simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   673
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   674
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   675
lemmas bin_rsplit_size_sign = bin_rsplit_size_sign' [OF asm_rl
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   676
  rev_rev_ident [THEN trans] set_rev [THEN equalityD2 [THEN subsetD]]]
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   677
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   678
lemma bin_nth_rsplit [rule_format] :
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   679
  "n > 0 \<Longrightarrow> m < n \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   680
    \<forall>w k nw.
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   681
      rev sw = bin_rsplit n (nw, w) \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   682
      k < size sw \<longrightarrow> bin_nth (sw ! k) m = bin_nth w (k * n + m)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   683
  apply (induct sw)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   684
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   685
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   686
  apply (drule bthrs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   687
  apply (simp (no_asm_use) add: Let_def split: prod.split_asm if_split_asm)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   688
  apply (erule allE, erule impE, erule exI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   689
  apply (case_tac k)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   690
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   691
   prefer 2
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   692
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   693
   apply (erule allE)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   694
   apply (erule (1) impE)
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   695
   apply (simp add: bit_drop_bit_eq ac_simps)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   696
  apply (simp add: bit_take_bit_iff ac_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   697
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   698
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   699
lemma bin_rsplit_all: "0 < nw \<Longrightarrow> nw \<le> n \<Longrightarrow> bin_rsplit n (nw, w) = [bintrunc n w]"
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   700
  by (auto simp: bin_rsplit_def rsplit_aux_simp2ls split: prod.split dest!: split_bintrunc)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   701
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   702
lemma bin_rsplit_l [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   703
  "\<forall>bin. bin_rsplitl n (m, bin) = bin_rsplit n (m, bintrunc m bin)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   704
  apply (rule_tac a = "m" in wf_less_than [THEN wf_induct])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   705
  apply (simp (no_asm) add: bin_rsplitl_def bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   706
  apply (rule allI)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   707
  apply (subst bin_rsplitl_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   708
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   709
  apply (clarsimp simp: Let_def split: prod.split)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   710
  apply (simp add: ac_simps)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   711
  apply (subst rsplit_aux_alts(1))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   712
  apply (subst rsplit_aux_alts(2))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   713
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   714
  unfolding bin_rsplit_def bin_rsplitl_def
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   715
  apply (simp add: drop_bit_take_bit)
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   716
  apply (case_tac \<open>x < n\<close>)
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   717
  apply (simp_all add: not_less min_def)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   718
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   719
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   720
lemma bin_rsplit_rcat [rule_format]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   721
  "n > 0 \<longrightarrow> bin_rsplit n (n * size ws, bin_rcat n ws) = map (bintrunc n) ws"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   722
  apply (unfold bin_rsplit_def bin_rcat_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   723
  apply (rule_tac xs = ws in rev_induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   724
   apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   725
  apply clarsimp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   726
  apply (subst rsplit_aux_alts)
71947
476b9e6904d9 replaced mere alias by input abbreviation
haftmann
parents: 71946
diff changeset
   727
  apply (simp add: drop_bit_bin_cat_eq take_bit_bin_cat_eq)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   728
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   729
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   730
lemma bin_rsplit_aux_len_le [rule_format] :
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   731
  "\<forall>ws m. n \<noteq> 0 \<longrightarrow> ws = bin_rsplit_aux n nw w bs \<longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   732
    length ws \<le> m \<longleftrightarrow> nw + length bs * n \<le> m * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   733
proof -
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   734
  have *: R
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   735
    if d: "i \<le> j \<or> m < j'"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   736
    and R1: "i * k \<le> j * k \<Longrightarrow> R"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   737
    and R2: "Suc m * k' \<le> j' * k' \<Longrightarrow> R"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   738
    for i j j' k k' m :: nat and R
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   739
    using d
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   740
    apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   741
    apply (rule R1, erule mult_le_mono1)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   742
    apply (rule R2, erule Suc_le_eq [THEN iffD2 [THEN mult_le_mono1]])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   743
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   744
  have **: "0 < sc \<Longrightarrow> sc - n + (n + lb * n) \<le> m * n \<longleftrightarrow> sc + lb * n \<le> m * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   745
    for sc m n lb :: nat
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   746
    apply safe
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   747
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   748
    apply (case_tac "sc \<ge> n")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   749
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   750
    apply (insert linorder_le_less_linear [of m lb])
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   751
    apply (erule_tac k=n and k'=n in *)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   752
     apply arith
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   753
    apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   754
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   755
  show ?thesis
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   756
    apply (induct n nw w bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   757
    apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   758
    apply (simp add: ** Let_def split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   759
    done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   760
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   761
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   762
lemma bin_rsplit_len_le: "n \<noteq> 0 \<longrightarrow> ws = bin_rsplit n (nw, w) \<longrightarrow> length ws \<le> m \<longleftrightarrow> nw \<le> m * n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   763
  by (auto simp: bin_rsplit_def bin_rsplit_aux_len_le)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   764
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   765
lemma bin_rsplit_aux_len:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   766
  "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit_aux n nw w cs) = (nw + n - 1) div n + length cs"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   767
  apply (induct n nw w cs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   768
  apply (subst bin_rsplit_aux.simps)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   769
  apply (clarsimp simp: Let_def split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   770
  apply (erule thin_rl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   771
  apply (case_tac m)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   772
   apply simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   773
  apply (case_tac "m \<le> n")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   774
   apply (auto simp add: div_add_self2)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   775
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   776
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   777
lemma bin_rsplit_len: "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, w)) = (nw + n - 1) div n"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   778
  by (auto simp: bin_rsplit_def bin_rsplit_aux_len)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   779
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   780
lemma bin_rsplit_aux_len_indep:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   781
  "n \<noteq> 0 \<Longrightarrow> length bs = length cs \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   782
    length (bin_rsplit_aux n nw v bs) =
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   783
    length (bin_rsplit_aux n nw w cs)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   784
proof (induct n nw w cs arbitrary: v bs rule: bin_rsplit_aux.induct)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   785
  case (1 n m w cs v bs)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   786
  show ?case
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   787
  proof (cases "m = 0")
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   788
    case True
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   789
    with \<open>length bs = length cs\<close> show ?thesis by simp
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   790
  next
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   791
    case False
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   792
    from "1.hyps" [of \<open>bin_split n w\<close> \<open>drop_bit n w\<close> \<open>take_bit n w\<close>] \<open>m \<noteq> 0\<close> \<open>n \<noteq> 0\<close>
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   793
    have hyp: "\<And>v bs. length bs = Suc (length cs) \<Longrightarrow>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   794
      length (bin_rsplit_aux n (m - n) v bs) =
71944
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   795
      length (bin_rsplit_aux n (m - n) (drop_bit n w) (take_bit n w # cs))"
18357df1cd20 avoid compound operation
haftmann
parents: 71943
diff changeset
   796
      using bin_rsplit_aux_len by fastforce 
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   797
    from \<open>length bs = length cs\<close> \<open>n \<noteq> 0\<close> show ?thesis
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   798
      by (auto simp add: bin_rsplit_aux_simp_alt Let_def bin_rsplit_len split: prod.split)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   799
  qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   800
qed
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   801
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   802
lemma bin_rsplit_len_indep:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   803
  "n \<noteq> 0 \<Longrightarrow> length (bin_rsplit n (nw, v)) = length (bin_rsplit n (nw, w))"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   804
  apply (unfold bin_rsplit_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   805
  apply (simp (no_asm))
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   806
  apply (erule bin_rsplit_aux_len_indep)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   807
  apply (rule refl)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   808
  done
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   809
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
   810
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   811
subsection \<open>Logical operations\<close>
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   812
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   813
primrec bin_sc :: "nat \<Rightarrow> bool \<Rightarrow> int \<Rightarrow> int"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   814
  where
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   815
    Z: "bin_sc 0 b w = of_bool b + 2 * bin_rest w"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   816
  | Suc: "bin_sc (Suc n) b w = of_bool (odd w) + 2 * bin_sc n b (w div 2)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   817
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   818
lemma bin_nth_sc [simp]: "bit (bin_sc n b w) n \<longleftrightarrow> b"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   819
  by (induction n arbitrary: w) (simp_all add: bit_Suc)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   820
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   821
lemma bin_sc_sc_same [simp]: "bin_sc n c (bin_sc n b w) = bin_sc n c w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   822
  by (induction n arbitrary: w) (simp_all add: bit_Suc)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   823
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   824
lemma bin_sc_sc_diff: "m \<noteq> n \<Longrightarrow> bin_sc m c (bin_sc n b w) = bin_sc n b (bin_sc m c w)"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   825
  apply (induct n arbitrary: w m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   826
   apply (case_tac [!] m)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   827
     apply auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   828
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   829
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   830
lemma bin_nth_sc_gen: "bin_nth (bin_sc n b w) m = (if m = n then b else bin_nth w m)"
71949
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   831
  apply (induct n arbitrary: w m)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   832
   apply (case_tac m; simp add: bit_Suc)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   833
  apply (case_tac m; simp add: bit_Suc)
5b8b1183c641 dropped yet another duplicate
haftmann
parents: 71947
diff changeset
   834
  done
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   835
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   836
lemma bin_sc_eq:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   837
  \<open>bin_sc n False = unset_bit n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   838
  \<open>bin_sc n True = Bit_Operations.set_bit n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   839
  by (simp_all add: fun_eq_iff bit_eq_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   840
    (simp_all add: bin_nth_sc_gen bit_set_bit_iff bit_unset_bit_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   841
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   842
lemma bin_sc_nth [simp]: "bin_sc n (bin_nth w n) w = w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   843
  by (rule bit_eqI) (simp add: bin_nth_sc_gen)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   844
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   845
lemma bin_sign_sc [simp]: "bin_sign (bin_sc n b w) = bin_sign w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   846
proof (induction n arbitrary: w)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   847
  case 0
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   848
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   849
    by (auto simp add: bin_sign_def) (use bin_rest_ge_0 in fastforce)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   850
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   851
  case (Suc n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   852
  from Suc [of \<open>w div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   853
  show ?case by (auto simp add: bin_sign_def split: if_splits)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   854
qed
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   855
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   856
lemma bin_sc_bintr [simp]:
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   857
  "bintrunc m (bin_sc n x (bintrunc m w)) = bintrunc m (bin_sc n x w)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   858
  apply (cases x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   859
   apply (simp_all add: bin_sc_eq bit_eq_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   860
   apply (auto simp add: bit_take_bit_iff bit_set_bit_iff bit_unset_bit_iff)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   861
  done
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   862
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   863
lemma bin_clr_le: "bin_sc n False w \<le> w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   864
  by (simp add: bin_sc_eq unset_bit_less_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   865
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   866
lemma bin_set_ge: "bin_sc n True w \<ge> w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   867
  by (simp add: bin_sc_eq set_bit_greater_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   868
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   869
lemma bintr_bin_clr_le: "bintrunc n (bin_sc m False w) \<le> bintrunc n w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   870
  by (simp add: bin_sc_eq take_bit_unset_bit_eq unset_bit_less_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   871
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   872
lemma bintr_bin_set_ge: "bintrunc n (bin_sc m True w) \<ge> bintrunc n w"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   873
  by (simp add: bin_sc_eq take_bit_set_bit_eq set_bit_greater_eq)
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   874
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   875
lemma bin_sc_FP [simp]: "bin_sc n False 0 = 0"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   876
  by (induct n) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   877
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   878
lemma bin_sc_TM [simp]: "bin_sc n True (- 1) = - 1"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   879
  by (induct n) auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   880
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   881
lemmas bin_sc_simps = bin_sc.Z bin_sc.Suc bin_sc_TM bin_sc_FP
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   882
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   883
lemma bin_sc_minus: "0 < n \<Longrightarrow> bin_sc (Suc (n - 1)) b w = bin_sc n b w"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   884
  by auto
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   885
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   886
lemmas bin_sc_Suc_minus =
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   887
  trans [OF bin_sc_minus [symmetric] bin_sc.Suc]
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   888
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   889
lemma bin_sc_numeral [simp]:
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   890
  "bin_sc (numeral k) b w =
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   891
    of_bool (odd w) + 2 * bin_sc (pred_numeral k) b (w div 2)"
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   892
  by (simp add: numeral_eq_Suc)
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   893
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
   894
instantiation int :: semiring_bit_syntax
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
   895
begin
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
   896
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   897
definition [iff]: "i !! n \<longleftrightarrow> bin_nth i n"
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   898
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   899
definition "shiftl x n = x * 2 ^ n" for x :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   900
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   901
definition "shiftr x n = x div 2 ^ n" for x :: int
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   902
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
   903
instance by standard
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
   904
  (simp_all add: fun_eq_iff shiftl_int_def shiftr_int_def push_bit_eq_mult drop_bit_eq_div)
25762
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
   905
c03e9d04b3e4 splitted class uminus from class minus
haftmann
parents: 25112
diff changeset
   906
end
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
   907
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   908
lemma shiftl_eq_push_bit:
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   909
  \<open>k << n = push_bit n k\<close> for k :: int
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
   910
  by (fact shiftl_eq_push_bit)
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   911
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   912
lemma shiftr_eq_drop_bit:
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   913
  \<open>k >> n = drop_bit n k\<close> for k :: int
72000
379d0c207c29 separation of traditional bit operations
haftmann
parents: 71997
diff changeset
   914
  by (fact shiftr_eq_drop_bit)
71943
d3fb19847662 formal relationships between operations
haftmann
parents: 71942
diff changeset
   915
70191
bdc835d934b7 no need to maintain two separate type classes
haftmann
parents: 70190
diff changeset
   916
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   917
subsubsection \<open>Basic simplification rules\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   918
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   919
lemmas int_not_def = not_int_def
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   920
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   921
lemma int_not_simps [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   922
  "NOT (0::int) = -1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
   923
  "NOT (1::int) = -2"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   924
  "NOT (- 1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   925
  "NOT (numeral w::int) = - numeral (w + Num.One)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   926
  "NOT (- numeral (Num.Bit0 w)::int) = numeral (Num.BitM w)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
   927
  "NOT (- numeral (Num.Bit1 w)::int) = numeral (Num.Bit0 w)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   928
  by (simp_all add: not_int_def)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   929
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   930
lemma int_not_not: "NOT (NOT x) = x"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   931
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   932
  by (fact bit.double_compl)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
   933
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   934
lemma int_and_0 [simp]: "0 AND x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   935
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   936
  by (fact bit.conj_zero_left)
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
   937
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   938
lemma int_and_m1 [simp]: "-1 AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   939
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   940
  by (fact bit.conj_one_left)
46019
507331bd8a08 remove recursion combinator bin_rec;
huffman
parents: 46018
diff changeset
   941
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   942
lemma int_or_zero [simp]: "0 OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   943
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   944
  by (fact bit.disj_zero_left)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
   945
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   946
lemma int_or_minus1 [simp]: "-1 OR x = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   947
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   948
  by (fact bit.disj_one_left)
46017
c5a1002161c3 simplify definition of OR for type int;
huffman
parents: 46016
diff changeset
   949
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   950
lemma int_xor_zero [simp]: "0 XOR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   951
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   952
  by (fact bit.xor_zero_left)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
   953
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   954
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   955
subsubsection \<open>Binary destructors\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   956
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   957
lemma bin_rest_NOT [simp]: "bin_rest (NOT x) = NOT (bin_rest x)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   958
  by (fact not_int_div_2)
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   959
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   960
lemma bin_last_NOT [simp]: "bin_last (NOT x) \<longleftrightarrow> \<not> bin_last x"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   961
  by simp
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   962
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   963
lemma bin_rest_AND [simp]: "bin_rest (x AND y) = bin_rest x AND bin_rest y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   964
  by (subst and_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   965
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   966
lemma bin_last_AND [simp]: "bin_last (x AND y) \<longleftrightarrow> bin_last x \<and> bin_last y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   967
  by (subst and_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   968
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   969
lemma bin_rest_OR [simp]: "bin_rest (x OR y) = bin_rest x OR bin_rest y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   970
  by (subst or_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   971
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   972
lemma bin_last_OR [simp]: "bin_last (x OR y) \<longleftrightarrow> bin_last x \<or> bin_last y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   973
  by (subst or_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   974
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   975
lemma bin_rest_XOR [simp]: "bin_rest (x XOR y) = bin_rest x XOR bin_rest y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   976
  by (subst xor_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   977
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   978
lemma bin_last_XOR [simp]: "bin_last (x XOR y) \<longleftrightarrow> (bin_last x \<or> bin_last y) \<and> \<not> (bin_last x \<and> bin_last y)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
   979
  by (subst xor_int_rec) auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   980
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   981
lemma bin_nth_ops:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   982
  "\<And>x y. bin_nth (x AND y) n \<longleftrightarrow> bin_nth x n \<and> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   983
  "\<And>x y. bin_nth (x OR y) n \<longleftrightarrow> bin_nth x n \<or> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   984
  "\<And>x y. bin_nth (x XOR y) n \<longleftrightarrow> bin_nth x n \<noteq> bin_nth y n"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   985
  "\<And>x. bin_nth (NOT x) n \<longleftrightarrow> \<not> bin_nth x n"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   986
  by (simp_all add: bit_and_iff bit_or_iff bit_xor_iff bit_not_iff)
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   987
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   988
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
   989
subsubsection \<open>Derived properties\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   990
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   991
lemma int_xor_minus1 [simp]: "-1 XOR x = NOT x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   992
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   993
  by (fact bit.xor_one_left)
46018
0bb66de5a0bf simplify definition of XOR for type int;
huffman
parents: 46017
diff changeset
   994
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   995
lemma int_xor_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   996
  "w XOR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   997
  "w XOR -1 = NOT w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
   998
  for w :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
   999
  by simp_all
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1000
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1001
lemma int_or_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1002
  "w OR 0 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1003
  "w OR -1 = -1"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1004
  for w :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1005
  by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1006
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
  1007
lemma int_and_extra_simps [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1008
  "w AND 0 = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1009
  "w AND -1 = w"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1010
  for w :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1011
  by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1012
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1013
text \<open>Commutativity of the above.\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1014
lemma bin_ops_comm:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1015
  fixes x y :: int
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1016
  shows int_and_comm: "x AND y = y AND x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1017
    and int_or_comm:  "x OR y = y OR x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1018
    and int_xor_comm: "x XOR y = y XOR x"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1019
  by (simp_all add: ac_simps)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1020
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1021
lemma bin_ops_same [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1022
  "x AND x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1023
  "x OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1024
  "x XOR x = 0"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1025
  for x :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1026
  by simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1027
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1028
lemmas bin_log_esimps =
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1029
  int_and_extra_simps  int_or_extra_simps  int_xor_extra_simps
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1030
  int_and_0 int_and_m1 int_or_zero int_or_minus1 int_xor_zero int_xor_minus1
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1031
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1032
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1033
subsubsection \<open>Basic properties of logical (bit-wise) operations\<close>
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1034
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1035
lemma bbw_ao_absorb: "x AND (y OR x) = x \<and> x OR (y AND x) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1036
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1037
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1038
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1039
lemma bbw_ao_absorbs_other:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1040
  "x AND (x OR y) = x \<and> (y AND x) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1041
  "(y OR x) AND x = x \<and> x OR (x AND y) = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1042
  "(x OR y) AND x = x \<and> (x AND y) OR x = x"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1043
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1044
  by (auto simp add: bin_eq_iff bin_nth_ops)
24353
9a7a9b19e925 use overloaded bitwise operators at type int
huffman
parents: 24350
diff changeset
  1045
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1046
lemmas bbw_ao_absorbs [simp] = bbw_ao_absorb bbw_ao_absorbs_other
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1047
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1048
lemma int_xor_not: "(NOT x) XOR y = NOT (x XOR y) \<and> x XOR (NOT y) = NOT (x XOR y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1049
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1050
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1051
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1052
lemma int_and_assoc: "(x AND y) AND z = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1053
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1054
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1055
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1056
lemma int_or_assoc: "(x OR y) OR z = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1057
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1058
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1059
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1060
lemma int_xor_assoc: "(x XOR y) XOR z = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1061
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1062
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1063
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1064
lemmas bbw_assocs = int_and_assoc int_or_assoc int_xor_assoc
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1065
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1066
(* BH: Why are these declared as simp rules??? *)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1067
lemma bbw_lcs [simp]:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1068
  "y AND (x AND z) = x AND (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1069
  "y OR (x OR z) = x OR (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1070
  "y XOR (x XOR z) = x XOR (y XOR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1071
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1072
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1073
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1074
lemma bbw_not_dist:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1075
  "NOT (x OR y) = (NOT x) AND (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1076
  "NOT (x AND y) = (NOT x) OR (NOT y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1077
  for x y :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1078
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1079
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1080
lemma bbw_oa_dist: "(x AND y) OR z = (x OR z) AND (y OR z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1081
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1082
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1083
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1084
lemma bbw_ao_dist: "(x OR y) AND z = (x AND z) OR (y AND z)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1085
  for x y z :: int
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1086
  by (auto simp add: bin_eq_iff bin_nth_ops)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1087
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1088
(*
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1089
Why were these declared simp???
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1090
declare bin_ops_comm [simp] bbw_assocs [simp]
24367
3e29eafabe16 AC rules for bitwise logical operators no longer declared simp
huffman
parents: 24366
diff changeset
  1091
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1092
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1093
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1094
subsubsection \<open>Simplification with numerals\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1095
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1096
text \<open>Cases for \<open>0\<close> and \<open>-1\<close> are already covered by other simp rules.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1097
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1098
lemma bin_rest_neg_numeral_BitM [simp]:
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1099
  "bin_rest (- numeral (Num.BitM w)) = - numeral w"
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1100
  by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1101
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1102
lemma bin_last_neg_numeral_BitM [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
  1103
  "bin_last (- numeral (Num.BitM w))"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1104
  by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1105
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1106
(* FIXME: The rule sets below are very large (24 rules for each
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1107
  operator). Is there a simpler way to do this? *)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1109
lemma int_and_numerals [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1110
  "numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1111
  "numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1112
  "numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1113
  "numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1114
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1115
  "numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (numeral x AND - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1116
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (numeral x AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1117
  "numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x AND - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1118
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1119
  "- numeral (Num.Bit0 x) AND numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1120
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1121
  "- numeral (Num.Bit1 x) AND numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) AND numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1122
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1123
  "- numeral (Num.Bit0 x) AND - numeral (Num.Bit1 y) = (2 :: int) * (- numeral x AND - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1124
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit0 y) = (2 :: int) * (- numeral (x + Num.One) AND - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1125
  "- numeral (Num.Bit1 x) AND - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) AND - numeral (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1126
  "(1::int) AND numeral (Num.Bit0 y) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1127
  "(1::int) AND numeral (Num.Bit1 y) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1128
  "(1::int) AND - numeral (Num.Bit0 y) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1129
  "(1::int) AND - numeral (Num.Bit1 y) = 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1130
  "numeral (Num.Bit0 x) AND (1::int) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1131
  "numeral (Num.Bit1 x) AND (1::int) = 1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1132
  "- numeral (Num.Bit0 x) AND (1::int) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1133
  "- numeral (Num.Bit1 x) AND (1::int) = 1"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1134
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1135
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1136
lemma int_or_numerals [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1137
  "numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1138
  "numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1139
  "numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1140
  "numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1141
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1142
  "numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1143
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1144
  "numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x OR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1145
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1146
  "- numeral (Num.Bit0 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1147
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1148
  "- numeral (Num.Bit1 x) OR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1149
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1150
  "- numeral (Num.Bit0 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x OR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1151
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1152
  "- numeral (Num.Bit1 x) OR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral (x + Num.One) OR - numeral (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1153
  "(1::int) OR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1154
  "(1::int) OR numeral (Num.Bit1 y) = numeral (Num.Bit1 y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1155
  "(1::int) OR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1156
  "(1::int) OR - numeral (Num.Bit1 y) = - numeral (Num.Bit1 y)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1157
  "numeral (Num.Bit0 x) OR (1::int) = numeral (Num.Bit1 x)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1158
  "numeral (Num.Bit1 x) OR (1::int) = numeral (Num.Bit1 x)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1159
  "- numeral (Num.Bit0 x) OR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1160
  "- numeral (Num.Bit1 x) OR (1::int) = - numeral (Num.Bit1 x)"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1161
  by (rule bin_rl_eqI; simp)+
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1162
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1163
lemma int_xor_numerals [simp]:
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1164
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1165
  "numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1166
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1167
  "numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1168
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (numeral x XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1169
  "numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (numeral x XOR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1170
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (numeral x XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1171
  "numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (2 :: int) * (numeral x XOR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1172
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1173
  "- numeral (Num.Bit0 x) XOR numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1174
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1175
  "- numeral (Num.Bit1 x) XOR numeral (Num.Bit1 y) = (2 :: int) * (- numeral (x + Num.One) XOR numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1176
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit0 y) = (2 :: int) * (- numeral x XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1177
  "- numeral (Num.Bit0 x) XOR - numeral (Num.Bit1 y) = 1 + (2 :: int) * (- numeral x XOR - numeral (y + Num.One))"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1178
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit0 y) = 1 + (2 :: int) * (- numeral (x + Num.One) XOR - numeral y)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1179
  "- numeral (Num.Bit1 x) XOR - numeral (Num.Bit1 y) = (2 :: int) * (- numeral (x + Num.One) XOR - numeral (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1180
  "(1::int) XOR numeral (Num.Bit0 y) = numeral (Num.Bit1 y)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1181
  "(1::int) XOR numeral (Num.Bit1 y) = numeral (Num.Bit0 y)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1182
  "(1::int) XOR - numeral (Num.Bit0 y) = - numeral (Num.BitM y)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1183
  "(1::int) XOR - numeral (Num.Bit1 y) = - numeral (Num.Bit0 (y + Num.One))"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1184
  "numeral (Num.Bit0 x) XOR (1::int) = numeral (Num.Bit1 x)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1185
  "numeral (Num.Bit1 x) XOR (1::int) = numeral (Num.Bit0 x)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1186
  "- numeral (Num.Bit0 x) XOR (1::int) = - numeral (Num.BitM x)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54427
diff changeset
  1187
  "- numeral (Num.Bit1 x) XOR (1::int) = - numeral (Num.Bit0 (x + Num.One))"
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1188
  by (rule bin_rl_eqI; simp)+
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1189
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1190
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1191
subsubsection \<open>Interactions with arithmetic\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1192
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1193
lemma plus_and_or: "(x AND y) + (x OR y) = x + y" for x y :: int
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1194
proof (induction x arbitrary: y rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1195
  case zero
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1196
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1197
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1198
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1199
  case minus
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1200
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1201
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1202
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1203
  case (even x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1204
  from even.IH [of \<open>y div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1205
  show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1206
    by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1207
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1208
  case (odd x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1209
  from odd.IH [of \<open>y div 2\<close>]
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1210
  show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1211
    by (auto simp add: and_int_rec [of _ y] or_int_rec [of _ y] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1212
qed
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1213
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1214
lemma le_int_or: "bin_sign y = 0 \<Longrightarrow> x \<le> x OR y"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1215
  for x y :: int
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1216
  by (simp add: bin_sign_def or_greater_eq split: if_splits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1217
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1218
lemmas int_and_le =
53062
3af1a6020014 some vague grouping of related theorems, with slight tuning of headings and sorting out of dubious lemmas into separate theory
haftmann
parents: 47219
diff changeset
  1219
  xtrans(3) [OF bbw_ao_absorbs (2) [THEN conjunct2, symmetric] le_int_or]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
  1220
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1221
text \<open>Interaction between bit-wise and arithmetic: good example of \<open>bin_induction\<close>.\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46610
diff changeset
  1222
lemma bin_add_not: "x + NOT x = (-1::int)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1223
  by (simp add: not_int_def)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1224
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1225
lemma AND_mod: "x AND (2 ^ n - 1) = x mod 2 ^ n"
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1226
  for x :: int
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1227
  by (simp flip: take_bit_eq_mod add: take_bit_eq_mask mask_eq_exp_minus_1)
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1228
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1229
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1230
subsubsection \<open>Comparison\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1231
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1232
lemma AND_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1233
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1234
  assumes "0 \<le> x"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1235
  shows "0 \<le> x AND y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1236
  using assms by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1237
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1238
lemma OR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1239
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1240
  assumes "0 \<le> x" "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1241
  shows "0 \<le> x OR y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1242
  using assms by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1243
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1244
lemma XOR_lower [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1245
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1246
  assumes "0 \<le> x" "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1247
  shows "0 \<le> x XOR y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1248
  using assms by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1249
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1250
lemma AND_upper1 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1251
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1252
  assumes "0 \<le> x"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1253
  shows "x AND y \<le> x"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1254
  using assms by (induction x arbitrary: y rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1255
    (simp_all add: and_int_rec [of \<open>_ * 2\<close>] and_int_rec [of \<open>1 + _ * 2\<close>] add_increasing)
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1256
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1257
lemmas AND_upper1' [simp] = order_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1258
lemmas AND_upper1'' [simp] = order_le_less_trans [OF AND_upper1] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1259
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1260
lemma AND_upper2 [simp]: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1261
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1262
  assumes "0 \<le> y"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1263
  shows "x AND y \<le> y"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1264
  using assms AND_upper1 [of y x] by (simp add: ac_simps)
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1265
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1266
lemmas AND_upper2' [simp] = order_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1267
lemmas AND_upper2'' [simp] = order_le_less_trans [OF AND_upper2] \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1268
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1269
lemma OR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1270
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1271
  assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1272
  shows "x OR y < 2 ^ n"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1273
using assms proof (induction x arbitrary: y n rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1274
  case zero
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1275
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1276
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1277
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1278
  case minus
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1279
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1280
    by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1281
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1282
  case (even x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1283
  from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1284
  show ?case 
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1285
    by (cases n) (auto simp add: or_int_rec [of \<open>_ * 2\<close>] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1286
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1287
  case (odd x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1288
  from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1289
  show ?case
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1290
    by (cases n) (auto simp add: or_int_rec [of \<open>1 + _ * 2\<close>], linarith)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1291
qed
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1292
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1293
lemma XOR_upper: \<^marker>\<open>contributor \<open>Stefan Berghofer\<close>\<close>
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1294
  fixes x y :: int
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1295
  assumes "0 \<le> x" "x < 2 ^ n" "y < 2 ^ n"
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1296
  shows "x XOR y < 2 ^ n"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1297
using assms proof (induction x arbitrary: y n rule: int_bit_induct)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1298
  case zero
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1299
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1300
    by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1301
next
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1302
  case minus
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1303
  then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1304
    by simp
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1305
next
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1306
  case (even x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1307
  from even.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] even.prems even.hyps
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1308
  show ?case 
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1309
    by (cases n) (auto simp add: xor_int_rec [of \<open>_ * 2\<close>] elim: oddE)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1310
next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1311
  case (odd x)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1312
  from odd.IH [of \<open>n - 1\<close> \<open>y div 2\<close>] odd.prems odd.hyps
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1313
  show ?case
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1314
    by (cases n) (auto simp add: xor_int_rec [of \<open>1 + _ * 2\<close>])
70172
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1315
qed
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1316
c247bf924d25 integrated Bit_Comparison into Word corpus
haftmann
parents: 70170
diff changeset
  1317
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1318
subsubsection \<open>Truncating results of bit-wise operations\<close>
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1319
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
  1320
lemma bin_trunc_ao:
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1321
  "bintrunc n x AND bintrunc n y = bintrunc n (x AND y)"
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1322
  "bintrunc n x OR bintrunc n y = bintrunc n (x OR y)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1323
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1324
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1325
lemma bin_trunc_xor: "bintrunc n (bintrunc n x XOR bintrunc n y) = bintrunc n (x XOR y)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1326
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1327
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1328
lemma bin_trunc_not: "bintrunc n (NOT (bintrunc n x)) = bintrunc n (NOT x)"
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
  1329
  by (auto simp add: bin_eq_iff bin_nth_ops nth_bintr)
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1330
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1331
text \<open>Want theorems of the form of \<open>bin_trunc_xor\<close>.\<close>
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1332
lemma bintr_bintr_i: "x = bintrunc n y \<Longrightarrow> bintrunc n x = bintrunc n y"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1333
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1334
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1335
lemmas bin_trunc_and = bin_trunc_ao(1) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1336
lemmas bin_trunc_or = bin_trunc_ao(2) [THEN bintr_bintr_i]
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1337
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1338
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1339
subsubsection \<open>More lemmas\<close>
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1340
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1341
lemma not_int_cmp_0 [simp]:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1342
  fixes i :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1343
  "0 < NOT i \<longleftrightarrow> i < -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1344
  "0 \<le> NOT i \<longleftrightarrow> i < 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1345
  "NOT i < 0 \<longleftrightarrow> i \<ge> 0"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1346
  "NOT i \<le> 0 \<longleftrightarrow> i \<ge> -1"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1347
by(simp_all add: int_not_def) arith+
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1348
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1349
lemma bbw_ao_dist2: "(x :: int) AND (y OR z) = x AND y OR x AND z"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1350
  by (fact bit.conj_disj_distrib)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1351
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1352
lemmas int_and_ac = bbw_lcs(1) int_and_comm int_and_assoc
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1353
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1354
lemma int_nand_same [simp]: fixes x :: int shows "x AND NOT x = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1355
  by simp
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1356
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1357
lemma int_nand_same_middle: fixes x :: int shows "x AND y AND NOT x = 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1358
  by (simp add: bit_eq_iff bit_and_iff bit_not_iff)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1359
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1360
lemma and_xor_dist: fixes x :: int shows
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1361
  "x AND (y XOR z) = (x AND y) XOR (x AND z)"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1362
  by (fact bit.conj_xor_distrib)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1363
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1364
lemma int_and_lt0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1365
  \<open>x AND y < 0 \<longleftrightarrow> x < 0 \<and> y < 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1366
  by (fact and_negative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1367
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1368
lemma int_and_ge0 [simp]: 
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1369
  \<open>x AND y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<or> y \<ge> 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1370
  by (fact and_nonnegative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1371
  
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1372
lemma int_and_1: fixes x :: int shows "x AND 1 = x mod 2"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1373
  by (fact and_one_eq)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1374
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1375
lemma int_1_and: fixes x :: int shows "1 AND x = x mod 2"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1376
  by (fact one_and_eq)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1377
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1378
lemma int_or_lt0 [simp]: 
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1379
  \<open>x OR y < 0 \<longleftrightarrow> x < 0 \<or> y < 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1380
  by (fact or_negative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1381
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1382
lemma int_or_ge0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1383
  \<open>x OR y \<ge> 0 \<longleftrightarrow> x \<ge> 0 \<and> y \<ge> 0\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1384
  by (fact or_nonnegative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1385
  
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1386
lemma int_xor_lt0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1387
  \<open>x XOR y < 0 \<longleftrightarrow> (x < 0) \<noteq> (y < 0)\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1388
  by (fact xor_negative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1389
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1390
lemma int_xor_ge0 [simp]:
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1391
  \<open>x XOR y \<ge> 0 \<longleftrightarrow> (x \<ge> 0 \<longleftrightarrow> y \<ge> 0)\<close> for x y :: int
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1392
  by (fact xor_nonnegative_int_iff)
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1393
  
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1394
lemma even_conv_AND:
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1395
  \<open>even i \<longleftrightarrow> i AND 1 = 0\<close> for i :: int
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1396
  by (simp add: and_one_eq mod2_eq_if)
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1397
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1398
lemma bin_last_conv_AND:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1399
  "bin_last i \<longleftrightarrow> i AND 1 \<noteq> 0"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1400
  by (simp add: and_one_eq mod2_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1401
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1402
lemma bitval_bin_last:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1403
  "of_bool (bin_last i) = i AND 1"
71957
3e162c63371a build bit operations on word on library theory on bit operations
haftmann
parents: 71949
diff changeset
  1404
  by (simp add: and_one_eq mod2_eq_if)
70190
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1405
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1406
lemma bin_sign_and:
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1407
  "bin_sign (i AND j) = - (bin_sign i * bin_sign j)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1408
by(simp add: bin_sign_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1409
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1410
lemma int_not_neg_numeral: "NOT (- numeral n) = (Num.sub n num.One :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1411
by(simp add: int_not_def)
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1412
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1413
lemma int_neg_numeral_pOne_conv_not: "- numeral (n + num.One) = (NOT (numeral n) :: int)"
ff9efdc84289 clarified structure of theories
haftmann
parents: 70183
diff changeset
  1414
by(simp add: int_not_def)
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1415
67120
491fd7f0b5df misc tuning and modernization;
wenzelm
parents: 65363
diff changeset
  1416
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 58874
diff changeset
  1417
subsection \<open>Setting and clearing bits\<close>
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24353
diff changeset
  1418
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1419
lemma int_shiftl_BIT: fixes x :: int
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1420
  shows int_shiftl0 [simp]: "x << 0 = x"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1421
  and int_shiftl_Suc [simp]: "x << Suc n = 2 * (x << n)"
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1422
  by (auto simp add: shiftl_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1423
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1424
lemma int_0_shiftl [simp]: "0 << n = (0 :: int)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1425
by(induct n) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1426
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1427
lemma bin_last_shiftl: "bin_last (x << n) \<longleftrightarrow> n = 0 \<and> bin_last x"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1428
by(cases n)(simp_all)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1429
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1430
lemma bin_rest_shiftl: "bin_rest (x << n) = (if n > 0 then x << (n - 1) else bin_rest x)"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1431
by(cases n)(simp_all)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1432
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1433
lemma bin_nth_shiftl [simp]: "bin_nth (x << n) m \<longleftrightarrow> n \<le> m \<and> bin_nth x (m - n)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1434
  by (simp add: bit_push_bit_iff_int shiftl_eq_push_bit)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1435
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1436
lemma bin_last_shiftr: "odd (x >> n) \<longleftrightarrow> x !! n" for x :: int
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1437
  by (simp add: shiftr_eq_drop_bit bit_iff_odd_drop_bit)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1438
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1439
lemma bin_rest_shiftr [simp]: "bin_rest (x >> n) = x >> Suc n"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1440
  by (simp add: bit_eq_iff shiftr_eq_drop_bit drop_bit_Suc bit_drop_bit_eq drop_bit_half)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1441
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1442
lemma bin_nth_shiftr [simp]: "bin_nth (x >> n) m = bin_nth x (n + m)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1443
  by (simp add: shiftr_eq_drop_bit bit_drop_bit_eq)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1444
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1445
lemma bin_nth_conv_AND:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1446
  fixes x :: int shows 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1447
  "bin_nth x n \<longleftrightarrow> x AND (1 << n) \<noteq> 0"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1448
  by (simp add: bit_eq_iff)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1449
    (auto simp add: shiftl_eq_push_bit bit_and_iff bit_push_bit_iff bit_exp_iff)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1450
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1451
lemma int_shiftl_numeral [simp]: 
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1452
  "(numeral w :: int) << numeral w' = numeral (num.Bit0 w) << pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1453
  "(- numeral w :: int) << numeral w' = - numeral (num.Bit0 w) << pred_numeral w'"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1454
by(simp_all add: numeral_eq_Suc shiftl_int_def)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1455
  (metis add_One mult_inc semiring_norm(11) semiring_norm(13) semiring_norm(2) semiring_norm(6) semiring_norm(87))+
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1456
71945
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1457
lemma int_shiftl_One_numeral [simp]:
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1458
  "(1 :: int) << numeral w = 2 << pred_numeral w"
4b1264316270 replaced operation with weak abstraction by input abbreviation
haftmann
parents: 71944
diff changeset
  1459
  using int_shiftl_numeral [of Num.One w] by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1460
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1461
lemma shiftl_ge_0 [simp]: fixes i :: int shows "i << n \<ge> 0 \<longleftrightarrow> i \<ge> 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1462
by(induct n) simp_all
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1463
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1464
lemma shiftl_lt_0 [simp]: fixes i :: int shows "i << n < 0 \<longleftrightarrow> i < 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1465
by (metis not_le shiftl_ge_0)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1466
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1467
lemma int_shiftl_test_bit: "(n << i :: int) !! m \<longleftrightarrow> m \<ge> i \<and> n !! (m - i)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1468
  by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1469
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1470
lemma int_0shiftr [simp]: "(0 :: int) >> x = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1471
by(simp add: shiftr_int_def)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1472
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1473
lemma int_minus1_shiftr [simp]: "(-1 :: int) >> x = -1"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1474
by(simp add: shiftr_int_def div_eq_minus1)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1475
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1476
lemma int_shiftr_ge_0 [simp]: fixes i :: int shows "i >> n \<ge> 0 \<longleftrightarrow> i \<ge> 0"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1477
  by (simp add: shiftr_eq_drop_bit)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1478
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1479
lemma int_shiftr_lt_0 [simp]: fixes i :: int shows "i >> n < 0 \<longleftrightarrow> i < 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1480
by (metis int_shiftr_ge_0 not_less)
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1481
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1482
lemma int_shiftr_numeral [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1483
  "(1 :: int) >> numeral w' = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1484
  "(numeral num.One :: int) >> numeral w' = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1485
  "(numeral (num.Bit0 w) :: int) >> numeral w' = numeral w >> pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1486
  "(numeral (num.Bit1 w) :: int) >> numeral w' = numeral w >> pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1487
  "(- numeral (num.Bit0 w) :: int) >> numeral w' = - numeral w >> pred_numeral w'"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1488
  "(- numeral (num.Bit1 w) :: int) >> numeral w' = - numeral (Num.inc w) >> pred_numeral w'"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1489
  by (simp_all add: shiftr_eq_drop_bit numeral_eq_Suc add_One drop_bit_Suc)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1490
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1491
lemma int_shiftr_numeral_Suc0 [simp]:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1492
  "(1 :: int) >> Suc 0 = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1493
  "(numeral num.One :: int) >> Suc 0 = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1494
  "(numeral (num.Bit0 w) :: int) >> Suc 0 = numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1495
  "(numeral (num.Bit1 w) :: int) >> Suc 0 = numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1496
  "(- numeral (num.Bit0 w) :: int) >> Suc 0 = - numeral w"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1497
  "(- numeral (num.Bit1 w) :: int) >> Suc 0 = - numeral (Num.inc w)"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1498
  by (simp_all add: shiftr_eq_drop_bit drop_bit_Suc add_One)
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1499
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1500
lemma bin_nth_minus_p2:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1501
  assumes sign: "bin_sign x = 0"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1502
  and y: "y = 1 << n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1503
  and m: "m < n"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1504
  and x: "x < y"
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1505
  shows "bin_nth (x - y) m = bin_nth x m"
71986
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1506
proof -
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1507
  from sign y x have \<open>x \<ge> 0\<close> and \<open>y = 2 ^ n\<close> and \<open>x < 2 ^ n\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1508
    by (simp_all add: bin_sign_def shiftl_eq_push_bit push_bit_eq_mult split: if_splits)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1509
  from \<open>0 \<le> x\<close> \<open>x < 2 ^ n\<close> \<open>m < n\<close> have \<open>bit x m \<longleftrightarrow> bit (x - 2 ^ n) m\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1510
  proof (induction m arbitrary: x n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1511
    case 0
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1512
    then show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1513
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1514
  next
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1515
    case (Suc m)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1516
    moreover define q where \<open>q = n - 1\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1517
    ultimately have n: \<open>n = Suc q\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1518
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1519
    have \<open>(x - 2 ^ Suc q) div 2 = x div 2 - 2 ^ q\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1520
      by simp
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1521
    moreover from Suc.IH [of \<open>x div 2\<close> q] Suc.prems
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1522
    have \<open>bit (x div 2) m \<longleftrightarrow> bit (x div 2 - 2 ^ q) m\<close>
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1523
      by (simp add: n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1524
    ultimately show ?case
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1525
      by (simp add: bit_Suc n)
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1526
  qed
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1527
  with \<open>y = 2 ^ n\<close> show ?thesis
76193dd4aec8 factored out ancient numeral representation
haftmann
parents: 71984
diff changeset
  1528
    by simp
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1529
qed
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1530
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1531
lemma bin_clr_conv_NAND:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1532
  "bin_sc n False i = i AND NOT (1 << n)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1533
  by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1534
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1535
lemma bin_set_conv_OR:
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1536
  "bin_sc n True i = i OR (1 << n)"
71941
49af3d9a818c replaced mere alias by input abbreviation
haftmann
parents: 71826
diff changeset
  1537
  by (induct n arbitrary: i) (rule bin_rl_eqI; simp)+
70183
3ea80c950023 incorporated various material from the AFP into the distribution
haftmann
parents: 70175
diff changeset
  1538
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67408
diff changeset
  1539
end