src/HOL/Word/Bit_Representation.thy
author haftmann
Tue, 16 Apr 2019 19:50:05 +0000
changeset 70170 56727602d0a5
parent 70169 8bb835f10a39
permissions -rw-r--r--
prefer one theory for misc material
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/Bit_Representation.thy
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     2
    Author:     Jeremy Dawson, NICTA
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
     3
*)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
     5
section \<open>Integers as implicit bit strings\<close>
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     6
37658
df789294c77a more speaking names
haftmann
parents: 37654
diff changeset
     7
theory Bit_Representation
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
     8
  imports Main
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     9
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    10
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    11
lemma int_mod_lem: "0 < n \<Longrightarrow> 0 \<le> b \<and> b < n \<longleftrightarrow> b mod n = b"
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    12
  for b n :: int
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    13
  apply safe
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    14
    apply (erule (1) mod_pos_pos_trivial)
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    15
   apply (erule_tac [!] subst)
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    16
   apply auto
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    17
  done
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    18
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    19
lemma int_mod_ge: "a < n \<Longrightarrow> 0 < n \<Longrightarrow> a \<le> a mod n"
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    20
  for a n :: int
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    21
  by (metis dual_order.trans le_cases mod_pos_pos_trivial pos_mod_conj)
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    22
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    23
lemma int_mod_ge': "b < 0 \<Longrightarrow> 0 < n \<Longrightarrow> b + n \<le> b mod n"
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    24
  for b n :: int
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    25
  by (metis add_less_same_cancel2 int_mod_ge mod_add_self2)
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    26
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    27
lemma int_mod_le': "0 \<le> b - n \<Longrightarrow> b mod n \<le> b - n"
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    28
  for b n :: int
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    29
  by (metis minus_mod_self2 zmod_le_nonneg_dividend)
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    30
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    31
lemma emep1: "even n \<Longrightarrow> even d \<Longrightarrow> 0 \<le> d \<Longrightarrow> (n + 1) mod d = (n mod d) + 1"
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    32
  for n d :: int
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    33
  by (auto simp add: pos_zmod_mult_2 add.commute dvd_def)
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    34
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    35
lemma m1mod2k: "- 1 mod 2 ^ n = (2 ^ n - 1 :: int)"
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    36
  by (rule zmod_minus1) simp
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    37
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
    38
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 60867
diff changeset
    39
subsection \<open>Constructors and destructors for binary integers\<close>
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    40
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    41
definition Bit :: "int \<Rightarrow> bool \<Rightarrow> int"  (infixl "BIT" 90)
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    42
  where "k BIT b = (if b then 1 else 0) + k + k"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    43
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    44
lemma Bit_B0: "k BIT False = k + k"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    45
   by (simp add: Bit_def)
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
    46
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    47
lemma Bit_B1: "k BIT True = k + k + 1"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    48
   by (simp add: Bit_def)
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    49
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    50
lemma Bit_B0_2t: "k BIT False = 2 * k"
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
    51
  by (rule trans, rule Bit_B0) simp
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
    52
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    53
lemma Bit_B1_2t: "k BIT True = 2 * k + 1"
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
    54
  by (rule trans, rule Bit_B1) simp
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
    55
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
    56
lemma power_BIT: "2 ^ Suc n - 1 = (2 ^ n - 1) BIT True"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
    57
  by (simp add: Bit_B1)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
    58
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
    59
definition bin_last :: "int \<Rightarrow> bool"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    60
  where "bin_last w \<longleftrightarrow> w mod 2 = 1"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    61
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    62
lemma bin_last_odd: "bin_last = odd"
58645
94bef115c08f more foundational definition for predicate even
haftmann
parents: 58410
diff changeset
    63
  by (rule ext) (simp add: bin_last_def even_iff_mod_2_eq_zero)
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    64
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
    65
definition bin_rest :: "int \<Rightarrow> int"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    66
  where "bin_rest w = w div 2"
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    67
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    68
lemma bin_rl_simp [simp]: "bin_rest w BIT bin_last w = w"
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    69
  unfolding bin_rest_def bin_last_def Bit_def
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    70
  by (cases "w mod 2 = 0") (use div_mult_mod_eq [of w 2] in simp_all)
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    71
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
    72
lemma bin_rest_BIT [simp]: "bin_rest (x BIT b) = x"
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    73
  unfolding bin_rest_def Bit_def
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    74
  by (cases b) simp_all
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    75
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
    76
lemma bin_last_BIT [simp]: "bin_last (x BIT b) = b"
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    77
  unfolding bin_last_def Bit_def
54873
c92a0e6ba828 dropped junk
haftmann
parents: 54863
diff changeset
    78
  by (cases b) simp_all
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    79
45848
ec252975e82c declare BIT_eq_iff [iff]; remove unneeded lemmas
huffman
parents: 45847
diff changeset
    80
lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    81
  by (auto simp: Bit_def) arith+
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    82
45849
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    83
lemma BIT_bin_simps [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    84
  "numeral k BIT False = numeral (Num.Bit0 k)"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    85
  "numeral k BIT True = numeral (Num.Bit1 k)"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    86
  "(- numeral k) BIT False = - numeral (Num.Bit0 k)"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
    87
  "(- numeral k) BIT True = - numeral (Num.BitM k)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
    88
  unfolding numeral.simps numeral_BitM
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    89
  by (simp_all add: Bit_def del: arith_simps add_numeral_special diff_numeral_special)
45849
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    90
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    91
lemma BIT_special_simps [simp]:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    92
  shows "0 BIT False = 0"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    93
    and "0 BIT True = 1"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    94
    and "1 BIT False = 2"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    95
    and "1 BIT True = 3"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    96
    and "(- 1) BIT False = - 2"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    97
    and "(- 1) BIT True = - 1"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
    98
  by (simp_all add: Bit_def)
45849
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    99
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   100
lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> \<not> b"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   101
  by (auto simp: Bit_def) arith
53438
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
   102
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   103
lemma Bit_eq_m1_iff: "w BIT b = -1 \<longleftrightarrow> w = -1 \<and> b"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   104
  by (auto simp: Bit_def) arith
53438
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
   105
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   106
lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   107
  by (induct w) simp_all
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   109
lemma expand_BIT:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   110
  "numeral (Num.Bit0 w) = numeral w BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   111
  "numeral (Num.Bit1 w) = numeral w BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   112
  "- numeral (Num.Bit0 w) = (- numeral w) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   113
  "- numeral (Num.Bit1 w) = (- numeral (w + Num.One)) BIT True"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   114
  by (simp_all add: add_One BitM_inc)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   115
45851
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   116
lemma bin_last_numeral_simps [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   117
  "\<not> bin_last 0"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   118
  "bin_last 1"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54873
diff changeset
   119
  "bin_last (- 1)"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   120
  "bin_last Numeral1"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   121
  "\<not> bin_last (numeral (Num.Bit0 w))"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   122
  "bin_last (numeral (Num.Bit1 w))"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   123
  "\<not> bin_last (- numeral (Num.Bit0 w))"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   124
  "bin_last (- numeral (Num.Bit1 w))"
60867
86e7560e07d0 slight cleanup of lemmas
haftmann
parents: 59807
diff changeset
   125
  by (simp_all add: bin_last_def zmod_zminus1_eq_if) (auto simp add: divmod_def)
45851
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   126
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   127
lemma bin_rest_numeral_simps [simp]:
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   128
  "bin_rest 0 = 0"
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   129
  "bin_rest 1 = 0"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54873
diff changeset
   130
  "bin_rest (- 1) = - 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   131
  "bin_rest Numeral1 = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   132
  "bin_rest (numeral (Num.Bit0 w)) = numeral w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   133
  "bin_rest (numeral (Num.Bit1 w)) = numeral w"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   134
  "bin_rest (- numeral (Num.Bit0 w)) = - numeral w"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   135
  "bin_rest (- numeral (Num.Bit1 w)) = - numeral (w + Num.One)"
60867
86e7560e07d0 slight cleanup of lemmas
haftmann
parents: 59807
diff changeset
   136
  by (simp_all add: bin_rest_def zdiv_zminus1_eq_if) (auto simp add: divmod_def)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   137
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   138
lemma less_Bits: "v BIT b < w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> \<not> b \<and> c"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   139
  by (auto simp: Bit_def)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   140
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   141
lemma le_Bits: "v BIT b \<le> w BIT c \<longleftrightarrow> v < w \<or> v \<le> w \<and> (\<not> b \<or> c)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   142
  by (auto simp: Bit_def)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   143
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
   144
lemma pred_BIT_simps [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   145
  "x BIT False - 1 = (x - 1) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   146
  "x BIT True - 1 = x BIT False"
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
   147
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
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
   148
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
   149
lemma succ_BIT_simps [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   150
  "x BIT False + 1 = x BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   151
  "x BIT True + 1 = (x + 1) BIT False"
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
   152
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   153
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
   154
lemma add_BIT_simps [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   155
  "x BIT False + y BIT False = (x + y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   156
  "x BIT False + y BIT True = (x + y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   157
  "x BIT True + y BIT False = (x + y) BIT True"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   158
  "x BIT True + y BIT True = (x + y + 1) BIT False"
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
   159
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   160
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
   161
lemma mult_BIT_simps [simp]:
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   162
  "x BIT False * y = (x * y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   163
  "x * y BIT False = (x * y) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   164
  "x BIT True * y = (x * y) BIT False + y"
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
   165
  by (simp_all add: Bit_B0_2t Bit_B1_2t algebra_simps)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   166
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   167
lemma B_mod_2': "X = 2 \<Longrightarrow> (w BIT True) mod X = 1 \<and> (w BIT False) mod X = 0"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   168
  by (simp add: Bit_B0 Bit_B1)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   169
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   170
lemma bin_ex_rl: "\<exists>w b. w BIT b = bin"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   171
  by (metis bin_rl_simp)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   172
67142
nipkow
parents: 65363
diff changeset
   173
lemma bin_exhaust: "(\<And>x b. bin = x BIT b \<Longrightarrow> Q) \<Longrightarrow> Q"
nipkow
parents: 65363
diff changeset
   174
by (metis bin_ex_rl)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   175
67160
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   176
primrec bin_nth :: "int \<Rightarrow> nat \<Rightarrow> bool"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   177
  where
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   178
    Z: "bin_nth w 0 \<longleftrightarrow> bin_last w"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   179
  | Suc: "bin_nth w (Suc n) \<longleftrightarrow> bin_nth (bin_rest w) n"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   180
67160
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   181
lemma bin_nth_eq_mod:
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   182
  "bin_nth w n \<longleftrightarrow> odd (w div 2 ^ n)"
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   183
  by (induction n arbitrary: w) (simp_all add: bin_last_def bin_rest_def odd_iff_mod_2_eq_one zdiv_zmult2_eq)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   184
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   185
lemma bin_abs_lem: "bin = (w BIT b) \<Longrightarrow> bin \<noteq> -1 \<longrightarrow> bin \<noteq> 0 \<longrightarrow> nat \<bar>w\<bar> < nat \<bar>bin\<bar>"
46598
fd0ac848140e simplify proof
huffman
parents: 46025
diff changeset
   186
  apply clarsimp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   187
  apply (unfold Bit_def)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   188
  apply (cases b)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   189
   apply (clarsimp, arith)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   190
  apply (clarsimp, arith)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   191
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   192
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   193
lemma bin_induct:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   194
  assumes PPls: "P 0"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54873
diff changeset
   195
    and PMin: "P (- 1)"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   196
    and PBit: "\<And>bin bit. P bin \<Longrightarrow> P (bin BIT bit)"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   197
  shows "P bin"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   198
  apply (rule_tac P=P and a=bin and f1="nat \<circ> abs" in wf_measure [THEN wf_induct])
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   199
  apply (simp add: measure_def inv_image_def)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   200
  apply (case_tac x rule: bin_exhaust)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   201
  apply (frule bin_abs_lem)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   202
  apply (auto simp add : PPls PMin PBit)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   203
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   204
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   205
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w"
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
   206
  unfolding bin_rest_def [symmetric] by (rule bin_rest_BIT)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   207
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   208
lemma bin_nth_eq_iff: "bin_nth x = bin_nth y \<longleftrightarrow> x = y"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   209
proof -
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   210
  have bin_nth_lem [rule_format]: "\<forall>y. bin_nth x = bin_nth y \<longrightarrow> x = y"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   211
    apply (induct x rule: bin_induct)
46008
c296c75f4cf4 reverted some changes for set->predicate transition, according to "hg log -u berghofe -r Isabelle2007:Isabelle2008";
wenzelm
parents: 46001
diff changeset
   212
      apply safe
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   213
      apply (erule rev_mp)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   214
      apply (induct_tac y rule: bin_induct)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   215
        apply safe
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   216
        apply (drule_tac x=0 in fun_cong, force)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   217
       apply (erule notE, rule ext, drule_tac x="Suc x" in fun_cong, force)
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   218
      apply (drule_tac x=0 in fun_cong, force)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   219
     apply (erule rev_mp)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   220
     apply (induct_tac y rule: bin_induct)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   221
       apply safe
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   222
       apply (drule_tac x=0 in fun_cong, force)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   223
      apply (erule notE, rule ext, drule_tac x="Suc x" in fun_cong, force)
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   224
     apply (metis Bit_eq_m1_iff Z bin_last_BIT)
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   225
    apply (case_tac y rule: bin_exhaust)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   226
    apply clarify
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   227
    apply (erule allE)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   228
    apply (erule impE)
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   229
     prefer 2
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   230
     apply (erule conjI)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   231
     apply (drule_tac x=0 in fun_cong, force)
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   232
    apply (rule ext)
59807
22bc39064290 prefer local fixes;
wenzelm
parents: 58874
diff changeset
   233
    apply (drule_tac x="Suc x" for x in fun_cong, force)
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   234
    done
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   235
  show ?thesis
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   236
    by (auto elim: bin_nth_lem)
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   237
qed
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   238
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   239
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   240
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   241
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   242
  using bin_nth_eq_iff by auto
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   243
45853
cbb6f2243b52 add lemma bin_nth_zero
huffman
parents: 45852
diff changeset
   244
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n"
cbb6f2243b52 add lemma bin_nth_zero
huffman
parents: 45852
diff changeset
   245
  by (induct n) auto
cbb6f2243b52 add lemma bin_nth_zero
huffman
parents: 45852
diff changeset
   246
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46001
diff changeset
   247
lemma bin_nth_1 [simp]: "bin_nth 1 n \<longleftrightarrow> n = 0"
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46001
diff changeset
   248
  by (cases n) simp_all
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46001
diff changeset
   249
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54873
diff changeset
   250
lemma bin_nth_minus1 [simp]: "bin_nth (- 1) n"
45952
ed9cc0634aaf add lemma bin_nth_minus1
huffman
parents: 45856
diff changeset
   251
  by (induct n) auto
ed9cc0634aaf add lemma bin_nth_minus1
huffman
parents: 45856
diff changeset
   252
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   253
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 \<longleftrightarrow> b"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   254
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   255
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   256
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   257
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   258
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   259
lemma bin_nth_minus [simp]: "0 < n \<Longrightarrow> bin_nth (w BIT b) n = bin_nth w (n - 1)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   260
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   261
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   262
lemma bin_nth_numeral: "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)"
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   263
  by (simp add: numeral_eq_Suc)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   264
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   265
lemmas bin_nth_numeral_simps [simp] =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   266
  bin_nth_numeral [OF bin_rest_numeral_simps(2)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   267
  bin_nth_numeral [OF bin_rest_numeral_simps(5)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   268
  bin_nth_numeral [OF bin_rest_numeral_simps(6)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   269
  bin_nth_numeral [OF bin_rest_numeral_simps(7)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   270
  bin_nth_numeral [OF bin_rest_numeral_simps(8)]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   271
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   272
lemmas bin_nth_simps =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   273
  bin_nth.Z bin_nth.Suc bin_nth_zero bin_nth_minus1
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   274
  bin_nth_numeral_simps
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   275
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   276
lemma nth_2p_bin: "bin_nth (2 ^ n) m = (m = n)" \<comment> \<open>for use when simplifying with \<open>bin_nth_Bit\<close>\<close>
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   277
  apply (induct n arbitrary: m)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   278
   apply clarsimp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   279
   apply safe
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   280
   apply (case_tac m)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   281
    apply (auto simp: Bit_B0_2t [symmetric])
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   282
  done 
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   283
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   284
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 60867
diff changeset
   285
subsection \<open>Truncating binary integers\<close>
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   286
54848
a303daddebbf syntactically tuned
haftmann
parents: 54847
diff changeset
   287
definition bin_sign :: "int \<Rightarrow> int"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   288
  where "bin_sign k = (if k \<ge> 0 then 0 else - 1)"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   289
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   290
lemma bin_sign_simps [simp]:
45850
50488b8abd58 add simp rules for bin_sign applied to numerals
huffman
parents: 45849
diff changeset
   291
  "bin_sign 0 = 0"
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46601
diff changeset
   292
  "bin_sign 1 = 0"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   293
  "bin_sign (- 1) = - 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   294
  "bin_sign (numeral k) = 0"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   295
  "bin_sign (- numeral k) = -1"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   296
  "bin_sign (w BIT b) = bin_sign w"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   297
  by (simp_all add: bin_sign_def Bit_def)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   298
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   299
lemma bin_sign_rest [simp]: "bin_sign (bin_rest w) = bin_sign w"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   300
  by (cases w rule: bin_exhaust) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   301
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   302
primrec bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   303
  where
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   304
    Z : "bintrunc 0 bin = 0"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   305
  | Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   306
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   307
primrec sbintrunc :: "nat \<Rightarrow> int \<Rightarrow> int"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   308
  where
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   309
    Z : "sbintrunc 0 bin = (if bin_last bin then -1 else 0)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   310
  | Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)"
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   311
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   312
lemma bintrunc_mod2p: "bintrunc n w = w mod 2 ^ n"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   313
  by (induct n arbitrary: w) (auto simp add: bin_last_def bin_rest_def Bit_def zmod_zmult2_eq)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   314
67160
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   315
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n"
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   316
proof (induction n arbitrary: w)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   317
  case 0
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   318
  then show ?case
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   319
    by (auto simp add: bin_last_odd odd_iff_mod_2_eq_one)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   320
next
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   321
  case (Suc n)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   322
  moreover have "((bin_rest w + 2 ^ n) mod (2 * 2 ^ n) - 2 ^ n) BIT bin_last w =
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   323
    (w + 2 * 2 ^ n) mod (4 * 2 ^ n) - 2 * 2 ^ n"
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   324
  proof (cases w rule: parity_cases)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   325
    case even
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   326
    then show ?thesis
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   327
      by (simp add: bin_last_odd bin_rest_def Bit_B0_2t mult_mod_right)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   328
  next
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   329
    case odd
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   330
    then have "2 * (w div 2) = w - 1"
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   331
      using minus_mod_eq_mult_div [of w 2] by simp
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   332
    moreover have "(2 * 2 ^ n + w - 1) mod (2 * 2 * 2 ^ n) + 1 = (2 * 2 ^ n + w) mod (2 * 2 * 2 ^ n)"
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   333
      using odd emep1 [of "2 * 2 ^ n + w - 1" "2 * 2 * 2 ^ n"] by simp
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   334
    ultimately show ?thesis 
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   335
      using odd by (simp add: bin_last_odd bin_rest_def Bit_B1_2t mult_mod_right) (simp add: algebra_simps)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   336
  qed
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   337
  ultimately show ?case
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   338
    by simp
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   339
qed
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   340
64593
50c715579715 reoriented congruence rules in non-explosive direction
haftmann
parents: 64246
diff changeset
   341
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   342
subsection "Simplifications for (s)bintrunc"
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   343
67160
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   344
lemma sign_bintr: "bin_sign (bintrunc n w) = 0"
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   345
  by (simp add: bintrunc_mod2p bin_sign_def)
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   346
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   347
lemma bintrunc_n_0 [simp]: "bintrunc n 0 = 0"
67160
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   348
  by (simp add: bintrunc_mod2p)
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   349
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   350
lemma sbintrunc_n_0 [simp]: "sbintrunc n 0 = 0"
67160
f37bf261bdf6 avoid smt proofs in distribution
haftmann
parents: 67142
diff changeset
   351
  by (simp add: sbintrunc_mod2p)
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   352
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54873
diff changeset
   353
lemma sbintrunc_n_minus1 [simp]: "sbintrunc n (- 1) = -1"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   354
  by (induct n) auto
45856
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   355
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   356
lemma bintrunc_Suc_numeral:
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   357
  "bintrunc (Suc n) 1 = 1"
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54873
diff changeset
   358
  "bintrunc (Suc n) (- 1) = bintrunc n (- 1) BIT True"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   359
  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT False"
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   360
  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT True"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   361
  "bintrunc (Suc n) (- numeral (Num.Bit0 w)) = bintrunc n (- numeral w) BIT False"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   362
  "bintrunc (Suc n) (- numeral (Num.Bit1 w)) = bintrunc n (- numeral (w + Num.One)) BIT True"
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   363
  by simp_all
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   364
45856
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   365
lemma sbintrunc_0_numeral [simp]:
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   366
  "sbintrunc 0 1 = -1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   367
  "sbintrunc 0 (numeral (Num.Bit0 w)) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   368
  "sbintrunc 0 (numeral (Num.Bit1 w)) = -1"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   369
  "sbintrunc 0 (- numeral (Num.Bit0 w)) = 0"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   370
  "sbintrunc 0 (- numeral (Num.Bit1 w)) = -1"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   371
  by simp_all
45856
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   372
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   373
lemma sbintrunc_Suc_numeral:
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   374
  "sbintrunc (Suc n) 1 = 1"
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   375
  "sbintrunc (Suc n) (numeral (Num.Bit0 w)) = sbintrunc n (numeral w) BIT False"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   376
  "sbintrunc (Suc n) (numeral (Num.Bit1 w)) = sbintrunc n (numeral w) BIT True"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   377
  "sbintrunc (Suc n) (- numeral (Num.Bit0 w)) = sbintrunc n (- numeral w) BIT False"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   378
  "sbintrunc (Suc n) (- numeral (Num.Bit1 w)) = sbintrunc n (- numeral (w + Num.One)) BIT True"
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   379
  by simp_all
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   380
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   381
lemma bin_sign_lem: "(bin_sign (sbintrunc n bin) = -1) = bin_nth bin n"
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   382
  apply (induct n arbitrary: bin)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   383
  apply (case_tac bin rule: bin_exhaust, case_tac b, auto)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   384
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   385
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   386
lemma nth_bintr: "bin_nth (bintrunc m w) n \<longleftrightarrow> n < m \<and> bin_nth w n"
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   387
  apply (induct n arbitrary: w m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   388
   apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   389
  apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   390
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   391
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   392
lemma nth_sbintr: "bin_nth (sbintrunc m w) n = (if n < m then bin_nth w n else bin_nth w m)"
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   393
  apply (induct n arbitrary: w m)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   394
   apply (case_tac m)
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   395
    apply simp_all
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   396
  apply (case_tac m)
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   397
   apply simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   398
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   399
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   400
lemma bin_nth_Bit: "bin_nth (w BIT b) n \<longleftrightarrow> n = 0 \<and> b \<or> (\<exists>m. n = Suc m \<and> bin_nth w m)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   401
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   402
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   403
lemma bin_nth_Bit0:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   404
  "bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow>
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   405
    (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   406
  using bin_nth_Bit [where w="numeral w" and b="False"] by simp
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   407
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   408
lemma bin_nth_Bit1:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   409
  "bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow>
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   410
    n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   411
  using bin_nth_Bit [where w="numeral w" and b="True"] by simp
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   412
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   413
lemma bintrunc_bintrunc_l: "n \<le> m \<Longrightarrow> bintrunc m (bintrunc n w) = bintrunc n w"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   414
  by (rule bin_eqI) (auto simp: nth_bintr)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   415
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   416
lemma sbintrunc_sbintrunc_l: "n \<le> m \<Longrightarrow> sbintrunc m (sbintrunc n w) = sbintrunc n w"
32439
7a91c7bcfe7e tuned proofs
nipkow
parents: 30971
diff changeset
   417
  by (rule bin_eqI) (auto simp: nth_sbintr)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   418
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   419
lemma bintrunc_bintrunc_ge: "n \<le> m \<Longrightarrow> bintrunc n (bintrunc m w) = bintrunc n w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   420
  by (rule bin_eqI) (auto simp: nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   421
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   422
lemma bintrunc_bintrunc_min [simp]: "bintrunc m (bintrunc n w) = bintrunc (min m n) w"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   423
  by (rule bin_eqI) (auto simp: nth_bintr)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   424
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   425
lemma sbintrunc_sbintrunc_min [simp]: "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   426
  by (rule bin_eqI) (auto simp: nth_sbintr min.absorb1 min.absorb2)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   427
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   428
lemmas bintrunc_Pls =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   429
  bintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   430
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   431
lemmas bintrunc_Min [simp] =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   432
  bintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   433
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   434
lemmas bintrunc_BIT  [simp] =
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
   435
  bintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   436
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   437
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   438
  bintrunc_Suc_numeral
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   439
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   440
lemmas sbintrunc_Suc_Pls =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   441
  sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   442
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   443
lemmas sbintrunc_Suc_Min =
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   444
  sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   445
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   446
lemmas sbintrunc_Suc_BIT [simp] =
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
   447
  sbintrunc.Suc [where bin="w BIT b", simplified bin_last_BIT bin_rest_BIT] for w b
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   448
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   449
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   450
  sbintrunc_Suc_numeral
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   451
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   452
lemmas sbintrunc_Pls =
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   453
  sbintrunc.Z [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   454
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   455
lemmas sbintrunc_Min =
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   456
  sbintrunc.Z [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   457
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   458
lemmas sbintrunc_0_BIT_B0 [simp] =
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   459
  sbintrunc.Z [where bin="w BIT False", simplified bin_last_numeral_simps bin_rest_numeral_simps]
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   460
  for w
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   461
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   462
lemmas sbintrunc_0_BIT_B1 [simp] =
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   463
  sbintrunc.Z [where bin="w BIT True", simplified bin_last_BIT bin_rest_numeral_simps]
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   464
  for w
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   465
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   466
lemmas sbintrunc_0_simps =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   467
  sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   468
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   469
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   470
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   471
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   472
lemma bintrunc_minus: "0 < n \<Longrightarrow> bintrunc (Suc (n - 1)) w = bintrunc n w"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   473
  by auto
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   474
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   475
lemma sbintrunc_minus: "0 < n \<Longrightarrow> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   476
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   477
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   478
lemmas bintrunc_minus_simps =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   479
  bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans]]
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   480
lemmas sbintrunc_minus_simps =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   481
  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   482
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   483
lemmas thobini1 = arg_cong [where f = "\<lambda>w. w BIT b"] for b
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   484
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   485
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   486
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   487
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   488
lemmas bmsts = bintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   489
lemmas bintrunc_Pls_minus_I = bmsts(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   490
lemmas bintrunc_Min_minus_I = bmsts(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   491
lemmas bintrunc_BIT_minus_I = bmsts(3)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   492
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   493
lemma bintrunc_Suc_lem: "bintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> bintrunc m x = y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   494
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   495
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   496
lemmas bintrunc_Suc_Ialts =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   497
  bintrunc_Min_I [THEN bintrunc_Suc_lem]
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   498
  bintrunc_BIT_I [THEN bintrunc_Suc_lem]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   499
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   500
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   501
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   502
lemmas sbintrunc_Suc_Is =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   503
  sbintrunc_Sucs(1-3) [THEN thobini1 [THEN [2] trans]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   504
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   505
lemmas sbintrunc_Suc_minus_Is =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   506
  sbintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   507
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   508
lemma sbintrunc_Suc_lem: "sbintrunc (Suc n) x = y \<Longrightarrow> m = Suc n \<Longrightarrow> sbintrunc m x = y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   509
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   510
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   511
lemmas sbintrunc_Suc_Ialts =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   512
  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   513
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   514
lemma sbintrunc_bintrunc_lt: "m > n \<Longrightarrow> sbintrunc n (bintrunc m w) = sbintrunc n w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   515
  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   516
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   517
lemma bintrunc_sbintrunc_le: "m \<le> Suc n \<Longrightarrow> bintrunc m (sbintrunc n w) = bintrunc m w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   518
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   519
  apply (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   520
   apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   521
  apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   522
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   523
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   524
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   525
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   526
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   527
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   528
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   529
lemma bintrunc_sbintrunc' [simp]: "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   530
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   531
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   532
lemma sbintrunc_bintrunc' [simp]: "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   533
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   534
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   535
lemma bin_sbin_eq_iff: "bintrunc (Suc n) x = bintrunc (Suc n) y \<longleftrightarrow> sbintrunc n x = sbintrunc n y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   536
  apply (rule iffI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   537
   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   538
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   539
  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   540
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   541
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   542
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   543
lemma bin_sbin_eq_iff':
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   544
  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y \<longleftrightarrow> sbintrunc (n - 1) x = sbintrunc (n - 1) y"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   545
  by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   546
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   547
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   548
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   549
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   550
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   551
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   552
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   553
(* although bintrunc_minus_simps, if added to default simpset,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   554
  tends to get applied where it's not wanted in developing the theories,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   555
  we get a version for when the word length is given literally *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   556
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   557
lemmas nat_non0_gr =
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   558
  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   559
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   560
lemma bintrunc_numeral:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   561
  "bintrunc (numeral k) x = bintrunc (pred_numeral k) (bin_rest x) BIT bin_last x"
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   562
  by (simp add: numeral_eq_Suc)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   563
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   564
lemma sbintrunc_numeral:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   565
  "sbintrunc (numeral k) x = sbintrunc (pred_numeral k) (bin_rest x) BIT bin_last x"
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   566
  by (simp add: numeral_eq_Suc)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   567
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   568
lemma bintrunc_numeral_simps [simp]:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   569
  "bintrunc (numeral k) (numeral (Num.Bit0 w)) = bintrunc (pred_numeral k) (numeral w) BIT False"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   570
  "bintrunc (numeral k) (numeral (Num.Bit1 w)) = bintrunc (pred_numeral k) (numeral w) BIT True"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   571
  "bintrunc (numeral k) (- numeral (Num.Bit0 w)) = bintrunc (pred_numeral k) (- numeral w) BIT False"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   572
  "bintrunc (numeral k) (- numeral (Num.Bit1 w)) =
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   573
    bintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   574
  "bintrunc (numeral k) 1 = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   575
  by (simp_all add: bintrunc_numeral)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   576
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   577
lemma sbintrunc_numeral_simps [simp]:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   578
  "sbintrunc (numeral k) (numeral (Num.Bit0 w)) = sbintrunc (pred_numeral k) (numeral w) BIT False"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   579
  "sbintrunc (numeral k) (numeral (Num.Bit1 w)) = sbintrunc (pred_numeral k) (numeral w) BIT True"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   580
  "sbintrunc (numeral k) (- numeral (Num.Bit0 w)) =
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   581
    sbintrunc (pred_numeral k) (- numeral w) BIT False"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   582
  "sbintrunc (numeral k) (- numeral (Num.Bit1 w)) =
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   583
    sbintrunc (pred_numeral k) (- numeral (w + Num.One)) BIT True"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   584
  "sbintrunc (numeral k) 1 = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   585
  by (simp_all add: sbintrunc_numeral)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   586
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
   587
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   588
  by (rule ext) (rule bintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   589
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   590
lemma range_bintrunc: "range (bintrunc n) = {i. 0 \<le> i \<and> i < 2 ^ n}"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   591
  apply (unfold no_bintr_alt1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   592
  apply (auto simp add: image_iff)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   593
  apply (rule exI)
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
   594
  apply (rule sym)
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
   595
  using int_mod_lem [symmetric, of "2 ^ n"]
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
   596
  apply auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   597
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   598
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   599
lemma no_sbintr_alt2: "sbintrunc n = (\<lambda>w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   600
  by (rule ext) (simp add : sbintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   601
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   602
lemma range_sbintrunc: "range (sbintrunc n) = {i. - (2 ^ n) \<le> i \<and> i < 2 ^ n}"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   603
  apply (unfold no_sbintr_alt2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   604
  apply (auto simp add: image_iff eq_diff_eq)
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
   605
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   606
  apply (rule exI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   607
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   608
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   609
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   610
lemma sb_inc_lem: "a + 2^k < 0 \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   611
  for a :: int
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
   612
  using int_mod_ge' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"]
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
   613
  by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   614
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   615
lemma sb_inc_lem': "a < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) \<le> (a + 2^k) mod 2^(Suc k)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   616
  for a :: int
35048
82ab78fff970 tuned proofs
haftmann
parents: 32642
diff changeset
   617
  by (rule sb_inc_lem) simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   618
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   619
lemma sbintrunc_inc: "x < - (2^n) \<Longrightarrow> x + 2^(Suc n) \<le> sbintrunc n x"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   620
  unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   621
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   622
lemma sb_dec_lem: "0 \<le> - (2 ^ k) + a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   623
  for a :: int
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53438
diff changeset
   624
  using int_mod_le'[where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   625
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   626
lemma sb_dec_lem': "2 ^ k \<le> a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   627
  for a :: int
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53438
diff changeset
   628
  by (rule sb_dec_lem) simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   629
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   630
lemma sbintrunc_dec: "x \<ge> (2 ^ n) \<Longrightarrow> x - 2 ^ (Suc n) >= sbintrunc n x"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   631
  unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   632
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   633
lemma bintr_ge0: "0 \<le> bintrunc n w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   634
  by (simp add: bintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   635
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   636
lemma bintr_lt2p: "bintrunc n w < 2 ^ n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   637
  by (simp add: bintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   638
58410
6d46ad54a2ab explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
haftmann
parents: 54873
diff changeset
   639
lemma bintr_Min: "bintrunc n (- 1) = 2 ^ n - 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   640
  by (simp add: bintrunc_mod2p m1mod2k)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   641
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   642
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   643
  by (simp add: sbintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   644
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   645
lemma sbintr_lt: "sbintrunc n w < 2 ^ n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   646
  by (simp add: sbintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   647
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   648
lemma sign_Pls_ge_0: "bin_sign bin = 0 \<longleftrightarrow> bin \<ge> 0"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   649
  for bin :: int
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   650
  by (simp add: bin_sign_def)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   651
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   652
lemma sign_Min_lt_0: "bin_sign bin = -1 \<longleftrightarrow> bin < 0"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   653
  for bin :: int
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   654
  by (simp add: bin_sign_def)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   655
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   656
lemma bin_rest_trunc: "bin_rest (bintrunc n bin) = bintrunc (n - 1) (bin_rest bin)"
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   657
  by (induct n arbitrary: bin) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   658
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
   659
lemma bin_rest_power_trunc:
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   660
  "(bin_rest ^^ k) (bintrunc n bin) = bintrunc (n - k) ((bin_rest ^^ k) bin)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   661
  by (induct k) (auto simp: bin_rest_trunc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   662
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   663
lemma bin_rest_trunc_i: "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   664
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   665
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   666
lemma bin_rest_strunc: "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   667
  by (induct n arbitrary: bin) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   668
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   669
lemma bintrunc_rest [simp]: "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   670
  apply (induct n arbitrary: bin)
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   671
   apply simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   672
  apply (case_tac bin rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   673
  apply (auto simp: bintrunc_bintrunc_l)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   674
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   675
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   676
lemma sbintrunc_rest [simp]: "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   677
  apply (induct n arbitrary: bin)
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   678
   apply simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   679
  apply (case_tac bin rule: bin_exhaust)
54847
d6cf9a5b9be9 prefer plain bool over dedicated type for binary digits
haftmann
parents: 54489
diff changeset
   680
  apply (auto simp: bintrunc_bintrunc_l split: bool.splits)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   681
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   682
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   683
lemma bintrunc_rest': "bintrunc n \<circ> bin_rest \<circ> bintrunc n = bin_rest \<circ> bintrunc n"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   684
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   685
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   686
lemma sbintrunc_rest': "sbintrunc n \<circ> bin_rest \<circ> sbintrunc n = bin_rest \<circ> sbintrunc n"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   687
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   688
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   689
lemma rco_lem: "f \<circ> g \<circ> f = g \<circ> f \<Longrightarrow> f \<circ> (g \<circ> f) ^^ n = g ^^ n \<circ> f"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   690
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   691
  apply (induct_tac n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   692
   apply (simp_all (no_asm))
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   693
  apply (drule fun_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   694
  apply (unfold o_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   695
  apply (erule trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   696
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   697
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   698
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   699
lemmas rco_bintr = bintrunc_rest'
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   700
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   701
lemmas rco_sbintr = sbintrunc_rest'
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   702
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   703
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   704
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 60867
diff changeset
   705
subsection \<open>Splitting and concatenation\<close>
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   706
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   707
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   708
  where
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   709
    Z: "bin_split 0 w = (w, 0)"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   710
  | Suc: "bin_split (Suc n) w =
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   711
      (let (w1, w2) = bin_split n (bin_rest w)
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   712
       in (w1, w2 BIT bin_last w))"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   713
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   714
lemma [code]:
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   715
  "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w) in (w1, w2 BIT bin_last w))"
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   716
  "bin_split 0 w = (w, 0)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   717
  by simp_all
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   718
65363
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   719
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int"
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   720
  where
5eb619751b14 misc tuning and modernization;
wenzelm
parents: 64593
diff changeset
   721
    Z: "bin_cat w 0 v = w"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   722
  | Suc: "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   723
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   724
lemma bin_sign_cat: "bin_sign (bin_cat x n y) = bin_sign x"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   725
  by (induct n arbitrary: y) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   726
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   727
lemma bin_cat_Suc_Bit: "bin_cat w (Suc n) (v BIT b) = bin_cat w n v BIT b"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   728
  by auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   729
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   730
lemma bin_cat_assoc: "bin_cat (bin_cat x m y) n z = bin_cat x (m + n) (bin_cat y n z)"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   731
  by (induct n arbitrary: z) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   732
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   733
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"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   734
  apply (induct n arbitrary: z m)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   735
   apply clarsimp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   736
  apply (case_tac m, auto)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   737
  done
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   738
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   739
definition bin_rcat :: "nat \<Rightarrow> int list \<Rightarrow> int"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   740
  where "bin_rcat n = foldl (\<lambda>u v. bin_cat u n v) 0"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   741
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   742
fun bin_rsplit_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   743
  where "bin_rsplit_aux n m c bs =
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   744
    (if m = 0 \<or> n = 0 then bs
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   745
     else
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   746
      let (a, b) = bin_split n c
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   747
      in bin_rsplit_aux n (m - n) a (b # bs))"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   748
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   749
definition bin_rsplit :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   750
  where "bin_rsplit n w = bin_rsplit_aux n (fst w) (snd w) []"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   751
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   752
fun bin_rsplitl_aux :: "nat \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int list \<Rightarrow> int list"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   753
  where "bin_rsplitl_aux n m c bs =
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   754
    (if m = 0 \<or> n = 0 then bs
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   755
     else
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   756
      let (a, b) = bin_split (min m n) c
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   757
      in bin_rsplitl_aux n (m - n) a (b # bs))"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   758
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   759
definition bin_rsplitl :: "nat \<Rightarrow> nat \<times> int \<Rightarrow> int list"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   760
  where "bin_rsplitl n w = bin_rsplitl_aux n (fst w) (snd w) []"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   761
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   762
declare bin_rsplit_aux.simps [simp del]
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   763
declare bin_rsplitl_aux.simps [simp del]
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   764
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   765
lemma bin_nth_cat:
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   766
  "bin_nth (bin_cat x k y) n =
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   767
    (if n < k then bin_nth y n else bin_nth x (n - k))"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   768
  apply (induct k arbitrary: n y)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   769
   apply clarsimp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   770
  apply (case_tac n, auto)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   771
  done
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   772
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   773
lemma bin_nth_split:
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   774
  "bin_split n c = (a, b) \<Longrightarrow>
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   775
    (\<forall>k. bin_nth a k = bin_nth c (n + k)) \<and>
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   776
    (\<forall>k. bin_nth b k = (k < n \<and> bin_nth c k))"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   777
  apply (induct n arbitrary: b c)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   778
   apply clarsimp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   779
  apply (clarsimp simp: Let_def split: prod.split_asm)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   780
  apply (case_tac k)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   781
  apply auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   782
  done
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   783
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   784
lemma bin_cat_zero [simp]: "bin_cat 0 n w = bintrunc n w"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   785
  by (induct n arbitrary: w) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   786
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   787
lemma bintr_cat1: "bintrunc (k + n) (bin_cat a n b) = bin_cat (bintrunc k a) n b"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   788
  by (induct n arbitrary: b) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   789
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   790
lemma bintr_cat: "bintrunc m (bin_cat a n b) =
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   791
    bin_cat (bintrunc (m - n) a) n (bintrunc (min m n) b)"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   792
  by (rule bin_eqI) (auto simp: bin_nth_cat nth_bintr)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   793
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   794
lemma bintr_cat_same [simp]: "bintrunc n (bin_cat a n b) = bintrunc n b"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   795
  by (auto simp add : bintr_cat)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   796
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   797
lemma cat_bintr [simp]: "bin_cat a n (bintrunc n b) = bin_cat a n b"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   798
  by (induct n arbitrary: b) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   799
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   800
lemma split_bintrunc: "bin_split n c = (a, b) \<Longrightarrow> b = bintrunc n c"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   801
  by (induct n arbitrary: b c) (auto simp: Let_def split: prod.split_asm)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   802
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   803
lemma bin_cat_split: "bin_split n w = (u, v) \<Longrightarrow> w = bin_cat u n v"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   804
  by (induct n arbitrary: v w) (auto simp: Let_def split: prod.split_asm)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   805
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   806
lemma bin_split_cat: "bin_split n (bin_cat v n w) = (v, bintrunc n w)"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   807
  by (induct n arbitrary: w) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   808
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   809
lemma bin_split_zero [simp]: "bin_split n 0 = (0, 0)"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   810
  by (induct n) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   811
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   812
lemma bin_split_minus1 [simp]:
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   813
  "bin_split n (- 1) = (- 1, bintrunc n (- 1))"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   814
  by (induct n) auto
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   815
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   816
lemma bin_split_trunc:
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   817
  "bin_split (min m n) c = (a, b) \<Longrightarrow>
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   818
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, b)"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   819
  apply (induct n arbitrary: m b c, clarsimp)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   820
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   821
  apply (case_tac m)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   822
   apply (auto simp: Let_def split: prod.split_asm)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   823
  done
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   824
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   825
lemma bin_split_trunc1:
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   826
  "bin_split n c = (a, b) \<Longrightarrow>
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   827
    bin_split n (bintrunc m c) = (bintrunc (m - n) a, bintrunc m b)"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   828
  apply (induct n arbitrary: m b c, clarsimp)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   829
  apply (simp add: bin_rest_trunc Let_def split: prod.split_asm)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   830
  apply (case_tac m)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   831
   apply (auto simp: Let_def split: prod.split_asm)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   832
  done
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   833
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   834
lemma bin_cat_num: "bin_cat a n b = a * 2 ^ n + bintrunc n b"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   835
  apply (induct n arbitrary: b)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   836
   apply clarsimp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   837
  apply (simp add: Bit_def)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   838
  done
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   839
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   840
lemma bin_split_num: "bin_split n b = (b div 2 ^ n, b mod 2 ^ n)"
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   841
  apply (induct n arbitrary: b)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   842
   apply simp
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   843
  apply (simp add: bin_rest_def zdiv_zmult2_eq)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   844
  apply (case_tac b rule: bin_exhaust)
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   845
  apply simp
70170
56727602d0a5 prefer one theory for misc material
haftmann
parents: 70169
diff changeset
   846
  apply (simp add: Bit_def mod_mult_mult1 pos_zmod_mult_2 add.commute)
70169
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   847
  done
8bb835f10a39 moved instance to appropriate place
haftmann
parents: 67160
diff changeset
   848
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
   849
end