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