src/HOL/Word/Bit_Representation.thy
author haftmann
Fri, 01 Nov 2013 18:51:14 +0100
changeset 54230 b1d955791529
parent 53438 6301ed01e34d
child 54489 03ff4d1e6784
permissions -rw-r--r--
more simplification rules on unary and binary minus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     1
(* 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     2
  Author: Jeremy Dawson, NICTA
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
*) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
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
     5
header {* Integers as implict bit strings *}
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
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
     8
imports "~~/src/HOL/Library/Bit" Misc_Numeric
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     9
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    10
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
    11
subsection {* Constructors and destructors for binary integers *}
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    12
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    13
definition bitval :: "bit \<Rightarrow> 'a\<Colon>zero_neq_one" where
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    14
  "bitval = bit_case 0 1"
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    15
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    16
lemma bitval_simps [simp]:
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    17
  "bitval 0 = 0"
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    18
  "bitval 1 = 1"
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    19
  by (simp_all add: bitval_def)
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    20
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    21
definition Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
    22
  "k BIT b = bitval b + k + k"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    23
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
    24
lemma Bit_B0:
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
    25
  "k BIT (0::bit) = k + k"
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
    26
   by (unfold Bit_def) 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
    27
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
    28
lemma Bit_B1:
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
    29
  "k BIT (1::bit) = k + k + 1"
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
    30
   by (unfold Bit_def) 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
    31
  
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
    32
lemma Bit_B0_2t: "k BIT (0::bit) = 2 * k"
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
    33
  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
    34
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
    35
lemma Bit_B1_2t: "k BIT (1::bit) = 2 * k + 1"
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
    36
  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
    37
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    38
definition bin_last :: "int \<Rightarrow> bit" where
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    39
  "bin_last w = (if w mod 2 = 0 then (0::bit) else (1::bit))"
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    40
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    41
definition bin_rest :: "int \<Rightarrow> int" where
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    42
  "bin_rest w = w div 2"
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    43
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    44
lemma bin_rl_simp [simp]:
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    45
  "bin_rest w BIT bin_last w = w"
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    46
  unfolding bin_rest_def bin_last_def Bit_def
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    47
  using mod_div_equality [of w 2]
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    48
  by (cases "w mod 2 = 0", simp_all)
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    49
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
    50
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
    51
  unfolding bin_rest_def Bit_def
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    52
  by (cases b, simp_all)
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    53
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
    54
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
    55
  unfolding bin_last_def Bit_def
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    56
  by (cases b, simp_all add: z1pmod2)
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    57
45848
ec252975e82c declare BIT_eq_iff [iff]; remove unneeded lemmas
huffman
parents: 45847
diff changeset
    58
lemma BIT_eq_iff [iff]: "u BIT b = v BIT c \<longleftrightarrow> u = v \<and> b = c"
45843
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    59
  by (metis bin_rest_BIT bin_last_BIT)
c58ce659ce2a reorder some definitions and proofs, in preparation for new numeral representation
huffman
parents: 45604
diff changeset
    60
45849
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    61
lemma BIT_bin_simps [simp]:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    62
  "numeral k BIT 0 = numeral (Num.Bit0 k)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    63
  "numeral k BIT 1 = numeral (Num.Bit1 k)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    64
  "neg_numeral k BIT 0 = neg_numeral (Num.Bit0 k)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    65
  "neg_numeral k BIT 1 = neg_numeral (Num.BitM k)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    66
  unfolding neg_numeral_def numeral.simps numeral_BitM
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    67
  unfolding Bit_def bitval_simps
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    68
  by (simp_all del: arith_simps add_numeral_special diff_numeral_special)
45849
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    69
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    70
lemma BIT_special_simps [simp]:
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    71
  shows "0 BIT 0 = 0" and "0 BIT 1 = 1" and "1 BIT 0 = 2" and "1 BIT 1 = 3"
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    72
  unfolding Bit_def by simp_all
904d8e0eaec6 add simp rules for BIT applied to numerals
huffman
parents: 45848
diff changeset
    73
53438
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
    74
lemma Bit_eq_0_iff: "w BIT b = 0 \<longleftrightarrow> w = 0 \<and> b = 0"
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
    75
  by (subst BIT_eq_iff [symmetric], simp)
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
    76
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
    77
lemma Bit_eq_m1_iff: "w BIT b = -1 \<longleftrightarrow> w = -1 \<and> b = 1"
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
    78
  by (subst BIT_eq_iff [symmetric], simp)
6301ed01e34d slight cleanup of lemma locations; tuned proof
haftmann
parents: 53062
diff changeset
    79
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    80
lemma BitM_inc: "Num.BitM (Num.inc w) = Num.Bit1 w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    81
  by (induct w, simp_all)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    82
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    83
lemma expand_BIT:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    84
  "numeral (Num.Bit0 w) = numeral w BIT 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    85
  "numeral (Num.Bit1 w) = numeral w BIT 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    86
  "neg_numeral (Num.Bit0 w) = neg_numeral w BIT 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    87
  "neg_numeral (Num.Bit1 w) = neg_numeral (w + Num.One) BIT 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    88
  unfolding add_One by (simp_all add: BitM_inc)
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    89
45851
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
    90
lemma bin_last_numeral_simps [simp]:
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
    91
  "bin_last 0 = 0"
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
    92
  "bin_last 1 = 1"
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
    93
  "bin_last -1 = 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    94
  "bin_last Numeral1 = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    95
  "bin_last (numeral (Num.Bit0 w)) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    96
  "bin_last (numeral (Num.Bit1 w)) = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    97
  "bin_last (neg_numeral (Num.Bit0 w)) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    98
  "bin_last (neg_numeral (Num.Bit1 w)) = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
    99
  unfolding expand_BIT bin_last_BIT by (simp_all add: bin_last_def)
45851
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   100
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   101
lemma bin_rest_numeral_simps [simp]:
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   102
  "bin_rest 0 = 0"
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   103
  "bin_rest 1 = 0"
19f7ac6cf3cc add simp rules for bin_rest and bin_last applied to numerals
huffman
parents: 45850
diff changeset
   104
  "bin_rest -1 = -1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   105
  "bin_rest Numeral1 = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   106
  "bin_rest (numeral (Num.Bit0 w)) = numeral w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   107
  "bin_rest (numeral (Num.Bit1 w)) = numeral w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   108
  "bin_rest (neg_numeral (Num.Bit0 w)) = neg_numeral w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   109
  "bin_rest (neg_numeral (Num.Bit1 w)) = neg_numeral (w + Num.One)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   110
  unfolding expand_BIT bin_rest_BIT by (simp_all add: bin_rest_def)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   111
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   112
lemma less_Bits: 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   113
  "(v BIT b < w BIT c) = (v < w | v <= w & b = (0::bit) & c = (1::bit))"
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   114
  unfolding Bit_def by (auto simp add: bitval_def split: bit.split)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   115
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   116
lemma le_Bits: 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   117
  "(v BIT b <= w BIT c) = (v < w | v <= w & (b ~= (1::bit) | c ~= (0::bit)))" 
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   118
  unfolding Bit_def by (auto simp add: bitval_def split: bit.split)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   119
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
   120
lemma pred_BIT_simps [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
   121
  "x BIT 0 - 1 = (x - 1) BIT 1"
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
   122
  "x BIT 1 - 1 = x BIT 0"
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
   123
  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
   124
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
   125
lemma succ_BIT_simps [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
   126
  "x BIT 0 + 1 = x BIT 1"
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
   127
  "x BIT 1 + 1 = (x + 1) BIT 0"
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
   128
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   129
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
   130
lemma add_BIT_simps [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
   131
  "x BIT 0 + y BIT 0 = (x + y) BIT 0"
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
   132
  "x BIT 0 + y BIT 1 = (x + y) BIT 1"
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
   133
  "x BIT 1 + y BIT 0 = (x + y) BIT 1"
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
   134
  "x BIT 1 + y BIT 1 = (x + y + 1) BIT 0"
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
   135
  by (simp_all add: Bit_B0_2t Bit_B1_2t)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   136
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
   137
lemma mult_BIT_simps [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
   138
  "x BIT 0 * y = (x * y) BIT 0"
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
   139
  "x * y BIT 0 = (x * y) BIT 0"
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
   140
  "x BIT 1 * y = (x * y) BIT 0 + y"
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
   141
  by (simp_all add: Bit_B0_2t Bit_B1_2t algebra_simps)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   142
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   143
lemma B_mod_2': 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   144
  "X = 2 ==> (w BIT (1::bit)) mod X = 1 & (w BIT (0::bit)) mod X = 0"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   145
  apply (simp (no_asm) only: Bit_B0 Bit_B1)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   146
  apply (simp add: z1pmod2)
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   147
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   148
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   149
lemma neB1E [elim!]:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   150
  assumes ne: "y \<noteq> (1::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   151
  assumes y: "y = (0::bit) \<Longrightarrow> P"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   152
  shows "P"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   153
  apply (rule y)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   154
  apply (cases y rule: bit.exhaust, simp)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   155
  apply (simp add: ne)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   156
  done
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   157
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   158
lemma bin_ex_rl: "EX w b. w BIT b = bin"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   159
  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
   160
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   161
lemma bin_exhaust:
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   162
  assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   163
  shows "Q"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   164
  apply (insert bin_ex_rl [of bin])  
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   165
  apply (erule exE)+
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   166
  apply (rule Q)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   167
  apply force
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   168
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   169
26514
eff55c0a6d34 tuned towards code generation
haftmann
parents: 26294
diff changeset
   170
primrec bin_nth where
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   171
  Z: "bin_nth w 0 = (bin_last w = (1::bit))"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   172
  | Suc: "bin_nth w (Suc n) = bin_nth (bin_rest w) n"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   173
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   174
lemma bin_abs_lem:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   175
  "bin = (w BIT b) ==> bin ~= -1 --> bin ~= 0 -->
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   176
    nat (abs w) < nat (abs bin)"
46598
fd0ac848140e simplify proof
huffman
parents: 46025
diff changeset
   177
  apply clarsimp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   178
  apply (unfold Bit_def)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   179
  apply (cases b)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   180
   apply (clarsimp, arith)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   181
  apply (clarsimp, arith)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   182
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   183
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   184
lemma bin_induct:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   185
  assumes PPls: "P 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   186
    and PMin: "P -1"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   187
    and PBit: "!!bin bit. P bin ==> P (bin BIT bit)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   188
  shows "P bin"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   189
  apply (rule_tac P=P and a=bin and f1="nat o abs" 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   190
                  in wf_measure [THEN wf_induct])
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   191
  apply (simp add: measure_def inv_image_def)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   192
  apply (case_tac x rule: bin_exhaust)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   193
  apply (frule bin_abs_lem)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   194
  apply (auto simp add : PPls PMin PBit)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   195
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   196
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   197
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
   198
  unfolding bin_rest_def [symmetric] by (rule bin_rest_BIT)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   199
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   200
lemma bin_nth_lem [rule_format]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   201
  "ALL y. bin_nth x = bin_nth y --> x = y"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   202
  apply (induct x rule: bin_induct)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   203
    apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   204
    apply (erule rev_mp)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   205
    apply (induct_tac y 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
   206
      apply safe
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   207
      apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   208
     apply (erule notE, rule ext, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   209
            drule_tac x="Suc x" in fun_cong, force)
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
   210
    apply (drule_tac x=0 in fun_cong, force)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   211
   apply (erule rev_mp)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   212
   apply (induct_tac y 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
   213
     apply safe
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   214
     apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   215
    apply (erule notE, rule ext, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   216
           drule_tac x="Suc x" in fun_cong, force)
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
   217
   apply (drule_tac x=0 in fun_cong, force)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   218
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   219
  apply clarify
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   220
  apply (erule allE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   221
  apply (erule impE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   222
   prefer 2
45848
ec252975e82c declare BIT_eq_iff [iff]; remove unneeded lemmas
huffman
parents: 45847
diff changeset
   223
   apply (erule conjI)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   224
   apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   225
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   226
  apply (drule_tac x="Suc ?x" in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   227
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   228
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   229
lemma bin_nth_eq_iff: "(bin_nth x = bin_nth y) = (x = y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   230
  by (auto elim: bin_nth_lem)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   231
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   232
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   233
45543
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   234
lemma bin_eq_iff: "x = y \<longleftrightarrow> (\<forall>n. bin_nth x n = bin_nth y n)"
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   235
  by (auto intro!: bin_nth_lem del: equalityI)
827bf668c822 HOL-Word: add simp rules for bin_rest, bin_last; shorten several proofs
huffman
parents: 45529
diff changeset
   236
45853
cbb6f2243b52 add lemma bin_nth_zero
huffman
parents: 45852
diff changeset
   237
lemma bin_nth_zero [simp]: "\<not> bin_nth 0 n"
cbb6f2243b52 add lemma bin_nth_zero
huffman
parents: 45852
diff changeset
   238
  by (induct n) auto
cbb6f2243b52 add lemma bin_nth_zero
huffman
parents: 45852
diff changeset
   239
46023
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46001
diff changeset
   240
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
   241
  by (cases n) simp_all
fad87bb608fc restate some lemmas to respect int/bin distinction
huffman
parents: 46001
diff changeset
   242
45952
ed9cc0634aaf add lemma bin_nth_minus1
huffman
parents: 45856
diff changeset
   243
lemma bin_nth_minus1 [simp]: "bin_nth -1 n"
ed9cc0634aaf add lemma bin_nth_minus1
huffman
parents: 45856
diff changeset
   244
  by (induct n) auto
ed9cc0634aaf add lemma bin_nth_minus1
huffman
parents: 45856
diff changeset
   245
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   246
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = (1::bit))"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   247
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   248
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   249
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   250
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   251
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   252
lemma bin_nth_minus [simp]: "0 < n ==> bin_nth (w BIT b) n = bin_nth w (n - 1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   253
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   254
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   255
lemma bin_nth_numeral:
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   256
  "bin_rest x = y \<Longrightarrow> bin_nth x (numeral n) = bin_nth y (pred_numeral n)"
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   257
  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
   258
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   259
lemmas bin_nth_numeral_simps [simp] =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   260
  bin_nth_numeral [OF bin_rest_numeral_simps(2)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   261
  bin_nth_numeral [OF bin_rest_numeral_simps(5)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   262
  bin_nth_numeral [OF bin_rest_numeral_simps(6)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   263
  bin_nth_numeral [OF bin_rest_numeral_simps(7)]
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   264
  bin_nth_numeral [OF bin_rest_numeral_simps(8)]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   265
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   266
lemmas bin_nth_simps = 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   267
  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
   268
  bin_nth_numeral_simps
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   269
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   270
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   271
subsection {* Truncating binary integers *}
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   272
45846
518a245a1ab6 type signature for bin_sign
huffman
parents: 45845
diff changeset
   273
definition bin_sign :: "int \<Rightarrow> int" where
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   274
  bin_sign_def: "bin_sign k = (if k \<ge> 0 then 0 else - 1)"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   275
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   276
lemma bin_sign_simps [simp]:
45850
50488b8abd58 add simp rules for bin_sign applied to numerals
huffman
parents: 45849
diff changeset
   277
  "bin_sign 0 = 0"
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46601
diff changeset
   278
  "bin_sign 1 = 0"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   279
  "bin_sign (numeral k) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   280
  "bin_sign (neg_numeral k) = -1"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   281
  "bin_sign (w BIT b) = bin_sign w"
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46601
diff changeset
   282
  unfolding bin_sign_def Bit_def bitval_def
45850
50488b8abd58 add simp rules for bin_sign applied to numerals
huffman
parents: 45849
diff changeset
   283
  by (simp_all split: bit.split)
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   284
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   285
lemma bin_sign_rest [simp]: 
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   286
  "bin_sign (bin_rest w) = bin_sign w"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   287
  by (cases w rule: bin_exhaust) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   288
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   289
primrec bintrunc :: "nat \<Rightarrow> int \<Rightarrow> int" where
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   290
  Z : "bintrunc 0 bin = 0"
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   291
| Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   292
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   293
primrec sbintrunc :: "nat => int => int" where
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   294
  Z : "sbintrunc 0 bin = (case bin_last bin of (1::bit) \<Rightarrow> -1 | (0::bit) \<Rightarrow> 0)"
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   295
| Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)"
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   296
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   297
lemma sign_bintr: "bin_sign (bintrunc n w) = 0"
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   298
  by (induct n arbitrary: w) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   299
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   300
lemma bintrunc_mod2p: "bintrunc n w = (w mod 2 ^ n)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   301
  apply (induct n arbitrary: w, clarsimp)
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   302
  apply (simp add: bin_last_def bin_rest_def Bit_def zmod_zmult2_eq)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   303
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   304
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   305
lemma sbintrunc_mod2p: "sbintrunc n w = (w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n"
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   306
  apply (induct n arbitrary: w)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   307
   apply simp
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   308
   apply (subst mod_add_left_eq)
45529
0e1037d4e049 remove redundant lemmas bin_last_mod and bin_rest_div, use bin_last_def and bin_rest_def instead
huffman
parents: 44939
diff changeset
   309
   apply (simp add: bin_last_def)
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   310
  apply (simp add: bin_last_def bin_rest_def Bit_def)
30940
663af91c0720 zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents: 30034
diff changeset
   311
  apply (clarsimp simp: mod_mult_mult1 [symmetric] 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   312
         zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   313
  apply (rule trans [symmetric, OF _ emep1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   314
     apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   315
  apply (auto simp: even_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   316
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   317
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   318
subsection "Simplifications for (s)bintrunc"
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   319
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   320
lemma bintrunc_n_0 [simp]: "bintrunc n 0 = 0"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   321
  by (induct n) auto
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   322
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   323
lemma sbintrunc_n_0 [simp]: "sbintrunc n 0 = 0"
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   324
  by (induct n) auto
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   325
45856
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   326
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
   327
  by (induct n) auto
45856
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   328
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   329
lemma bintrunc_Suc_numeral:
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   330
  "bintrunc (Suc n) 1 = 1"
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   331
  "bintrunc (Suc n) -1 = bintrunc n -1 BIT 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   332
  "bintrunc (Suc n) (numeral (Num.Bit0 w)) = bintrunc n (numeral w) BIT 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   333
  "bintrunc (Suc n) (numeral (Num.Bit1 w)) = bintrunc n (numeral w) BIT 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   334
  "bintrunc (Suc n) (neg_numeral (Num.Bit0 w)) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   335
    bintrunc n (neg_numeral w) BIT 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   336
  "bintrunc (Suc n) (neg_numeral (Num.Bit1 w)) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   337
    bintrunc n (neg_numeral (w + Num.One)) BIT 1"
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   338
  by simp_all
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   339
45856
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   340
lemma sbintrunc_0_numeral [simp]:
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   341
  "sbintrunc 0 1 = -1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   342
  "sbintrunc 0 (numeral (Num.Bit0 w)) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   343
  "sbintrunc 0 (numeral (Num.Bit1 w)) = -1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   344
  "sbintrunc 0 (neg_numeral (Num.Bit0 w)) = 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   345
  "sbintrunc 0 (neg_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
   346
  by simp_all
45856
caa99836aed8 more simp rules for sbintrunc
huffman
parents: 45855
diff changeset
   347
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   348
lemma sbintrunc_Suc_numeral:
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   349
  "sbintrunc (Suc n) 1 = 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   350
  "sbintrunc (Suc n) (numeral (Num.Bit0 w)) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   351
    sbintrunc n (numeral w) BIT 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   352
  "sbintrunc (Suc n) (numeral (Num.Bit1 w)) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   353
    sbintrunc n (numeral w) BIT 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   354
  "sbintrunc (Suc n) (neg_numeral (Num.Bit0 w)) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   355
    sbintrunc n (neg_numeral w) BIT 0"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   356
  "sbintrunc (Suc n) (neg_numeral (Num.Bit1 w)) =
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   357
    sbintrunc n (neg_numeral (w + Num.One)) BIT 1"
45855
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   358
  by simp_all
b49cffac6c97 add simp rules for sbintrunc applied to numerals
huffman
parents: 45853
diff changeset
   359
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   360
lemma bit_bool:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   361
  "(b = (b' = (1::bit))) = (b' = (if b then (1::bit) else (0::bit)))"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   362
  by (cases b') auto
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   363
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   364
lemmas bit_bool1 [simp] = refl [THEN bit_bool [THEN iffD1], symmetric]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   365
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   366
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
   367
  apply (induct n arbitrary: bin)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   368
  apply (case_tac bin rule: bin_exhaust, case_tac b, auto)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   369
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   370
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   371
lemma nth_bintr: "bin_nth (bintrunc m w) n = (n < m & bin_nth w n)"
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   372
  apply (induct n arbitrary: w m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   373
   apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   374
  apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   375
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   376
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   377
lemma nth_sbintr:
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   378
  "bin_nth (sbintrunc m w) n = 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   379
          (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
   380
  apply (induct n arbitrary: w m)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   381
   apply (case_tac m, simp_all split: bit.splits)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   382
  apply (case_tac m, simp_all split: bit.splits)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   383
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   384
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   385
lemma bin_nth_Bit:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   386
  "bin_nth (w BIT b) n = (n = 0 & b = (1::bit) | (EX m. n = Suc m & bin_nth w m))"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   387
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   388
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   389
lemma bin_nth_Bit0:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   390
  "bin_nth (numeral (Num.Bit0 w)) n \<longleftrightarrow>
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   391
    (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   392
  using bin_nth_Bit [where w="numeral w" and b="(0::bit)"] by simp
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   393
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   394
lemma bin_nth_Bit1:
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   395
  "bin_nth (numeral (Num.Bit1 w)) n \<longleftrightarrow>
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   396
    n = 0 \<or> (\<exists>m. n = Suc m \<and> bin_nth (numeral w) m)"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   397
  using bin_nth_Bit [where w="numeral w" and b="(1::bit)"] by simp
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   398
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   399
lemma bintrunc_bintrunc_l:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   400
  "n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   401
  by (rule bin_eqI) (auto simp add : nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   402
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   403
lemma sbintrunc_sbintrunc_l:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   404
  "n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)"
32439
7a91c7bcfe7e tuned proofs
nipkow
parents: 30971
diff changeset
   405
  by (rule bin_eqI) (auto simp: nth_sbintr)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   406
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   407
lemma bintrunc_bintrunc_ge:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   408
  "n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   409
  by (rule bin_eqI) (auto simp: nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   410
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   411
lemma bintrunc_bintrunc_min [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   412
  "bintrunc m (bintrunc n w) = bintrunc (min m n) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   413
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   414
  apply (auto simp: nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   415
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   416
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   417
lemma sbintrunc_sbintrunc_min [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   418
  "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   419
  apply (rule bin_eqI)
32642
026e7c6a6d08 be more cautious wrt. simp rules: inf_absorb1, inf_absorb2, sup_absorb1, sup_absorb2 are no simp rules by default any longer
haftmann
parents: 32439
diff changeset
   420
  apply (auto simp: nth_sbintr min_max.inf_absorb1 min_max.inf_absorb2)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   421
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   422
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   423
lemmas bintrunc_Pls = 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   424
  bintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   425
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   426
lemmas bintrunc_Min [simp] = 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   427
  bintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   428
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   429
lemmas bintrunc_BIT  [simp] = 
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
   430
  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
   431
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   432
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT
45852
24f563d94497 add simp rules for bintrunc applied to numerals
huffman
parents: 45851
diff changeset
   433
  bintrunc_Suc_numeral
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   434
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   435
lemmas sbintrunc_Suc_Pls = 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   436
  sbintrunc.Suc [where bin="0", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   437
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   438
lemmas sbintrunc_Suc_Min = 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   439
  sbintrunc.Suc [where bin="-1", simplified bin_last_numeral_simps bin_rest_numeral_simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   440
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   441
lemmas sbintrunc_Suc_BIT [simp] = 
46600
d6847e6b62db remove duplication of lemmas bin_{rest,last}_BIT
huffman
parents: 46599
diff changeset
   442
  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
   443
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   444
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
   445
  sbintrunc_Suc_numeral
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   446
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   447
lemmas sbintrunc_Pls = 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   448
  sbintrunc.Z [where bin="0", 
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   449
               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   450
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   451
lemmas sbintrunc_Min = 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   452
  sbintrunc.Z [where bin="-1",
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   453
               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   454
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   455
lemmas sbintrunc_0_BIT_B0 [simp] = 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   456
  sbintrunc.Z [where bin="w BIT (0::bit)", 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   457
               simplified bin_last_numeral_simps bin_rest_numeral_simps bit.simps] for w
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   458
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   459
lemmas sbintrunc_0_BIT_B1 [simp] = 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   460
  sbintrunc.Z [where bin="w BIT (1::bit)", 
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   461
               simplified bin_last_BIT bin_rest_numeral_simps bit.simps] for w
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   462
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   463
lemmas sbintrunc_0_simps =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   464
  sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   465
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   466
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   467
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   468
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   469
lemma bintrunc_minus:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   470
  "0 < n ==> bintrunc (Suc (n - 1)) w = bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   471
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   472
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   473
lemma sbintrunc_minus:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   474
  "0 < n ==> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   475
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   476
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   477
lemmas bintrunc_minus_simps = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   478
  bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   479
lemmas sbintrunc_minus_simps = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   480
  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   481
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   482
lemmas thobini1 = arg_cong [where f = "%w. w BIT b"] for b
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   483
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   484
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   485
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   486
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   487
lemmas bmsts = bintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans]]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   488
lemmas bintrunc_Pls_minus_I = bmsts(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   489
lemmas bintrunc_Min_minus_I = bmsts(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   490
lemmas bintrunc_BIT_minus_I = bmsts(3)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   491
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   492
lemma bintrunc_Suc_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   493
  "bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   494
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   495
e77ea0ea7f2c * HOL-Word:
kleing
parents:
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
e77ea0ea7f2c * HOL-Word:
kleing
parents:
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
e77ea0ea7f2c * HOL-Word:
kleing
parents:
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
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   508
lemma sbintrunc_Suc_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   509
  "sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   510
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   511
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   512
lemmas sbintrunc_Suc_Ialts = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   513
  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   514
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   515
lemma sbintrunc_bintrunc_lt:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   516
  "m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   517
  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   518
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   519
lemma bintrunc_sbintrunc_le:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   520
  "m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   521
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   522
  apply (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   523
   apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   524
  apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   525
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   526
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   527
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   528
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   529
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   530
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   531
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   532
lemma bintrunc_sbintrunc' [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   533
  "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   534
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   535
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   536
lemma sbintrunc_bintrunc' [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   537
  "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   538
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   539
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   540
lemma bin_sbin_eq_iff: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   541
  "bintrunc (Suc n) x = bintrunc (Suc n) y <-> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   542
   sbintrunc n x = sbintrunc n y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   543
  apply (rule iffI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   544
   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   545
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   546
  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   547
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   548
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   549
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   550
lemma bin_sbin_eq_iff':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   551
  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <-> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   552
            sbintrunc (n - 1) x = sbintrunc (n - 1) y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   553
  by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   554
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   555
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   556
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   557
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   558
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   559
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   560
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   561
(* although bintrunc_minus_simps, if added to default simpset,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   562
  tends to get applied where it's not wanted in developing the theories,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   563
  we get a version for when the word length is given literally *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   564
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   565
lemmas nat_non0_gr = 
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   566
  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl]
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:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   569
  "bintrunc (numeral k) x =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   570
    bintrunc (pred_numeral k) (bin_rest x) BIT bin_last x"
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   571
  by (simp add: numeral_eq_Suc)
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   572
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   573
lemma sbintrunc_numeral:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   574
  "sbintrunc (numeral k) x =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   575
    sbintrunc (pred_numeral k) (bin_rest x) BIT bin_last x"
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   576
  by (simp add: numeral_eq_Suc)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   577
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   578
lemma bintrunc_numeral_simps [simp]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   579
  "bintrunc (numeral k) (numeral (Num.Bit0 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   580
    bintrunc (pred_numeral k) (numeral w) BIT 0"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   581
  "bintrunc (numeral k) (numeral (Num.Bit1 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   582
    bintrunc (pred_numeral k) (numeral w) BIT 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   583
  "bintrunc (numeral k) (neg_numeral (Num.Bit0 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   584
    bintrunc (pred_numeral k) (neg_numeral w) BIT 0"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   585
  "bintrunc (numeral k) (neg_numeral (Num.Bit1 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   586
    bintrunc (pred_numeral k) (neg_numeral (w + Num.One)) BIT 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   587
  "bintrunc (numeral k) 1 = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   588
  by (simp_all add: bintrunc_numeral)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   589
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   590
lemma sbintrunc_numeral_simps [simp]:
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   591
  "sbintrunc (numeral k) (numeral (Num.Bit0 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   592
    sbintrunc (pred_numeral k) (numeral w) BIT 0"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   593
  "sbintrunc (numeral k) (numeral (Num.Bit1 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   594
    sbintrunc (pred_numeral k) (numeral w) BIT 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   595
  "sbintrunc (numeral k) (neg_numeral (Num.Bit0 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   596
    sbintrunc (pred_numeral k) (neg_numeral w) BIT 0"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   597
  "sbintrunc (numeral k) (neg_numeral (Num.Bit1 w)) =
47219
172c031ad743 restate various simp rules for word operations using pred_numeral
huffman
parents: 47170
diff changeset
   598
    sbintrunc (pred_numeral k) (neg_numeral (w + Num.One)) BIT 1"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   599
  "sbintrunc (numeral k) 1 = 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   600
  by (simp_all add: sbintrunc_numeral)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   601
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
   602
lemma no_bintr_alt1: "bintrunc n = (\<lambda>w. w mod 2 ^ n :: int)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   603
  by (rule ext) (rule bintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   604
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   605
lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   606
  apply (unfold no_bintr_alt1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   607
  apply (auto simp add: image_iff)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   608
  apply (rule exI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   609
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   610
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   611
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   612
lemma no_sbintr_alt2: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   613
  "sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   614
  by (rule ext) (simp add : sbintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   615
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   616
lemma range_sbintrunc: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   617
  "range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   618
  apply (unfold no_sbintr_alt2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   619
  apply (auto simp add: image_iff eq_diff_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   620
  apply (rule exI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   621
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   622
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   623
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   624
lemma sb_inc_lem:
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   625
  "(a::int) + 2^k < 0 \<Longrightarrow> a + 2^k + 2^(Suc k) <= (a + 2^k) mod 2^(Suc k)"
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   626
  apply (erule int_mod_ge' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k", simplified zless2p])
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   627
  apply (rule TrueI)
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   628
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   629
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   630
lemma sb_inc_lem':
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   631
  "(a::int) < - (2^k) \<Longrightarrow> a + 2^k + 2^(Suc k) <= (a + 2^k) mod 2^(Suc k)"
35048
82ab78fff970 tuned proofs
haftmann
parents: 32642
diff changeset
   632
  by (rule sb_inc_lem) simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   633
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   634
lemma sbintrunc_inc:
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   635
  "x < - (2^n) ==> x + 2^(Suc n) <= sbintrunc n x"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   636
  unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   637
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   638
lemma sb_dec_lem:
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53438
diff changeset
   639
  "(0::int) \<le> - (2 ^ k) + a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53438
diff changeset
   640
  using int_mod_le'[where n = "2 ^ (Suc k)" and b = "a + 2 ^ k"] by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   641
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   642
lemma sb_dec_lem':
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53438
diff changeset
   643
  "(2::int) ^ k \<le> a \<Longrightarrow> (a + 2 ^ k) mod (2 * 2 ^ k) \<le> - (2 ^ k) + a"
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53438
diff changeset
   644
  by (rule sb_dec_lem) simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   645
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   646
lemma sbintrunc_dec:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   647
  "x >= (2 ^ n) ==> x - 2 ^ (Suc n) >= sbintrunc n x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   648
  unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   649
47168
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   650
lemmas zmod_uminus' = zminus_zmod [where m=c] for c
47164
6a4c479ba94f generalized lemma zpower_zmod
huffman
parents: 47163
diff changeset
   651
lemmas zpower_zmod' = power_mod [where b=c and n=k] for c k
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   652
47168
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   653
lemmas brdmod1s' [symmetric] =
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   654
  mod_add_left_eq mod_add_right_eq
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   655
  mod_diff_left_eq mod_diff_right_eq
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   656
  mod_mult_left_eq mod_mult_right_eq
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   657
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   658
lemmas brdmods' [symmetric] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   659
  zpower_zmod' [symmetric]
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   660
  trans [OF mod_add_left_eq mod_add_right_eq] 
47168
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   661
  trans [OF mod_diff_left_eq mod_diff_right_eq] 
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   662
  trans [OF mod_mult_right_eq mod_mult_left_eq] 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   663
  zmod_uminus' [symmetric]
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   664
  mod_add_left_eq [where b = "1::int"]
47168
8395d7d63fc8 mark some duplicate lemmas for deletion
huffman
parents: 47164
diff changeset
   665
  mod_diff_left_eq [where b = "1::int"]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   666
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   667
lemmas bintr_arith1s =
46000
871bdab23f5c remove some uses of Int.succ and Int.pred
huffman
parents: 45999
diff changeset
   668
  brdmod1s' [where c="2^n::int", folded bintrunc_mod2p] for n
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   669
lemmas bintr_ariths =
46000
871bdab23f5c remove some uses of Int.succ and Int.pred
huffman
parents: 45999
diff changeset
   670
  brdmods' [where c="2^n::int", folded bintrunc_mod2p] for n
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   671
45604
29cf40fe8daf eliminated obsolete "standard";
wenzelm
parents: 45543
diff changeset
   672
lemmas m2pths = pos_mod_sign pos_mod_bound [OF zless2p]
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   673
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   674
lemma bintr_ge0: "0 \<le> bintrunc n w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   675
  by (simp add: bintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   676
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   677
lemma bintr_lt2p: "bintrunc n w < 2 ^ n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   678
  by (simp add: bintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   679
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   680
lemma bintr_Min: "bintrunc n -1 = 2 ^ n - 1"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   681
  by (simp add: bintrunc_mod2p m1mod2k)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   682
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   683
lemma sbintr_ge: "- (2 ^ n) \<le> sbintrunc n w"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   684
  by (simp add: sbintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   685
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   686
lemma sbintr_lt: "sbintrunc n w < 2 ^ n"
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   687
  by (simp add: sbintrunc_mod2p)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   688
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   689
lemma sign_Pls_ge_0: 
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46601
diff changeset
   690
  "(bin_sign bin = 0) = (bin >= (0 :: int))"
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46601
diff changeset
   691
  unfolding bin_sign_def by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   692
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   693
lemma sign_Min_lt_0: 
46604
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46601
diff changeset
   694
  "(bin_sign bin = -1) = (bin < (0 :: int))"
9f9e85264e4d make uses of bin_sign respect int/bin distinction
huffman
parents: 46601
diff changeset
   695
  unfolding bin_sign_def by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   696
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   697
lemma bin_rest_trunc:
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   698
  "(bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)"
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   699
  by (induct n arbitrary: bin) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   700
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
   701
lemma bin_rest_power_trunc:
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   702
  "(bin_rest ^^ k) (bintrunc n bin) = 
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   703
    bintrunc (n - k) ((bin_rest ^^ k) bin)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   704
  by (induct k) (auto simp: bin_rest_trunc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   705
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   706
lemma bin_rest_trunc_i:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   707
  "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   708
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   709
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   710
lemma bin_rest_strunc:
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   711
  "bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   712
  by (induct n arbitrary: bin) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   713
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   714
lemma bintrunc_rest [simp]: 
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   715
  "bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   716
  apply (induct n arbitrary: bin, simp)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   717
  apply (case_tac bin rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   718
  apply (auto simp: bintrunc_bintrunc_l)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   719
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   720
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   721
lemma sbintrunc_rest [simp]:
45954
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   722
  "sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
f67d3bb5f09c use 'induct arbitrary' instead of universal quantifiers
huffman
parents: 45953
diff changeset
   723
  apply (induct n arbitrary: bin, simp)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   724
  apply (case_tac bin rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   725
  apply (auto simp: bintrunc_bintrunc_l split: bit.splits)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   726
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   727
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   728
lemma bintrunc_rest':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   729
  "bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   730
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   731
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   732
lemma sbintrunc_rest' :
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   733
  "sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   734
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   735
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   736
lemma rco_lem:
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   737
  "f o g o f = g o f ==> f o (g o f) ^^ n = g ^^ n o f"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   738
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   739
  apply (induct_tac n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   740
   apply (simp_all (no_asm))
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   741
  apply (drule fun_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   742
  apply (unfold o_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   743
  apply (erule trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   744
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   745
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   746
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   747
lemmas rco_bintr = bintrunc_rest' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   748
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   749
lemmas rco_sbintr = sbintrunc_rest' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   750
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   751
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
   752
  
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   753
subsection {* Splitting and concatenation *}
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   754
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   755
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int" where
46001
0b562d564d5f redefine some binary operations on integers work on abstract numerals instead of Int.Pls and Int.Min
huffman
parents: 46000
diff changeset
   756
  Z: "bin_split 0 w = (w, 0)"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   757
  | Suc: "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   758
        in (w1, w2 BIT bin_last w))"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   759
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   760
lemma [code]:
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   761
  "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
   762
  "bin_split 0 w = (w, 0)"
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46607
diff changeset
   763
  by simp_all
37667
41acc0fa6b6c avoid bitstrings in generated code
haftmann
parents: 37658
diff changeset
   764
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   765
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" where
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   766
  Z: "bin_cat w 0 v = w"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   767
  | 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
   768
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
   769
end
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   770