src/HOL/Word/Bit_Representation.thy
author haftmann
Wed, 30 Jun 2010 16:45:47 +0200
changeset 37658 df789294c77a
parent 37654 src/HOL/Word/BinGeneral.thy@8e33b9d04a82
child 37667 41acc0fa6b6c
permissions -rw-r--r--
more speaking names
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
  contains basic definition to do with integers
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     5
  expressed using Pls, Min, BIT and important resulting theorems, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
  in particular, bin_rec and related work
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     7
*) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
     9
header {* Basic Definitions for Binary Integers *}
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    10
37658
df789294c77a more speaking names
haftmann
parents: 37654
diff changeset
    11
theory Bit_Representation
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    12
imports Misc_Numeric Bit
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    15
subsection {* Further properties of numerals *}
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    16
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    17
definition Bit :: "int \<Rightarrow> bit \<Rightarrow> int" (infixl "BIT" 90) where
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    18
  "k BIT b = bit_case 0 1 b + k + k"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    19
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    20
lemma BIT_B0_eq_Bit0 [simp]: "w BIT 0 = Int.Bit0 w"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    21
  unfolding Bit_def Bit0_def by simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    22
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    23
lemma BIT_B1_eq_Bit1 [simp]: "w BIT 1 = Int.Bit1 w"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    24
  unfolding Bit_def Bit1_def by simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    25
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    26
lemmas BIT_simps = BIT_B0_eq_Bit0 BIT_B1_eq_Bit1
24384
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    27
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    28
lemma Min_ne_Pls [iff]:  
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    29
  "Int.Min ~= Int.Pls"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    30
  unfolding Min_def Pls_def by auto
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    31
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    32
lemmas Pls_ne_Min [iff] = Min_ne_Pls [symmetric]
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    33
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    34
lemmas PlsMin_defs [intro!] = 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    35
  Pls_def Min_def Pls_def [symmetric] Min_def [symmetric]
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    36
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    37
lemmas PlsMin_simps [simp] = PlsMin_defs [THEN Eq_TrueI]
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    38
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    39
lemma number_of_False_cong: 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    40
  "False \<Longrightarrow> number_of x = number_of y"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    41
  by (rule FalseE)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    42
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    43
(** ways in which type Bin resembles a datatype **)
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    44
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    45
lemma BIT_eq: "u BIT b = v BIT c ==> u = v & b = c"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    46
  apply (unfold Bit_def)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    47
  apply (simp (no_asm_use) split: bit.split_asm)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    48
     apply simp_all
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    49
   apply (drule_tac f=even in arg_cong, clarsimp)+
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    50
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    51
     
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    52
lemmas BIT_eqE [elim!] = BIT_eq [THEN conjE, standard]
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    53
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    54
lemma BIT_eq_iff [simp]: 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    55
  "(u BIT b = v BIT c) = (u = v \<and> b = c)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    56
  by (rule iffI) auto
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    57
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    58
lemmas BIT_eqI [intro!] = conjI [THEN BIT_eq_iff [THEN iffD2]]
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    59
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    60
lemma less_Bits: 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    61
  "(v BIT b < w BIT c) = (v < w | v <= w & b = (0::bit) & c = (1::bit))"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    62
  unfolding Bit_def by (auto split: bit.split)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    63
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    64
lemma le_Bits: 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    65
  "(v BIT b <= w BIT c) = (v < w | v <= w & (b ~= (1::bit) | c ~= (0::bit)))" 
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    66
  unfolding Bit_def by (auto split: bit.split)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    67
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    68
lemma no_no [simp]: "number_of (number_of i) = i"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    69
  unfolding number_of_eq by simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    70
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    71
lemma Bit_B0:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    72
  "k BIT (0::bit) = k + k"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    73
   by (unfold Bit_def) simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    74
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    75
lemma Bit_B1:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    76
  "k BIT (1::bit) = k + k + 1"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    77
   by (unfold Bit_def) simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    78
  
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    79
lemma Bit_B0_2t: "k BIT (0::bit) = 2 * k"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    80
  by (rule trans, rule Bit_B0) simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    81
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    82
lemma Bit_B1_2t: "k BIT (1::bit) = 2 * k + 1"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    83
  by (rule trans, rule Bit_B1) simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    84
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    85
lemma B_mod_2': 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    86
  "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
    87
  apply (simp (no_asm) only: Bit_B0 Bit_B1)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    88
  apply (simp add: z1pmod2)
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
    89
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    90
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    91
lemma B1_mod_2 [simp]: "(Int.Bit1 w) mod 2 = 1"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    92
  unfolding numeral_simps number_of_is_id by (simp add: z1pmod2)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    93
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    94
lemma B0_mod_2 [simp]: "(Int.Bit0 w) mod 2 = 0"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    95
  unfolding numeral_simps number_of_is_id by simp
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
    96
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
    97
lemma neB1E [elim!]:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    98
  assumes ne: "y \<noteq> (1::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
    99
  assumes y: "y = (0::bit) \<Longrightarrow> P"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   100
  shows "P"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   101
  apply (rule y)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   102
  apply (cases y rule: bit.exhaust, simp)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   103
  apply (simp add: ne)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   104
  done
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   105
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   106
lemma bin_ex_rl: "EX w b. w BIT b = bin"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   107
  apply (unfold Bit_def)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   108
  apply (cases "even bin")
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   109
   apply (clarsimp simp: even_equiv_def)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   110
   apply (auto simp: odd_equiv_def split: bit.split)
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   111
  done
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   112
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   113
lemma bin_exhaust:
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   114
  assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   115
  shows "Q"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   116
  apply (insert bin_ex_rl [of bin])  
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   117
  apply (erule exE)+
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   118
  apply (rule Q)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   119
  apply force
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   120
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   121
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   122
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   123
subsection {* Destructors for binary integers *}
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   124
37546
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   125
definition bin_last :: "int \<Rightarrow> bit" where
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   126
  "bin_last w = (if w mod 2 = 0 then (0::bit) else (1::bit))"
37546
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   127
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   128
definition bin_rest :: "int \<Rightarrow> int" where
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   129
  "bin_rest w = w div 2"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   130
37546
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   131
definition bin_rl :: "int \<Rightarrow> int \<times> bit" where 
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   132
  "bin_rl w = (bin_rest w, bin_last w)"
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   133
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   134
lemma bin_rl_char: "bin_rl w = (r, l) \<longleftrightarrow> r BIT l = w"
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   135
  apply (cases l)
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   136
  apply (auto simp add: bin_rl_def bin_last_def bin_rest_def)
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   137
  unfolding Pls_def Min_def Bit0_def Bit1_def number_of_is_id
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   138
  apply arith+
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   139
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   140
26514
eff55c0a6d34 tuned towards code generation
haftmann
parents: 26294
diff changeset
   141
primrec bin_nth where
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   142
  Z: "bin_nth w 0 = (bin_last w = (1::bit))"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   143
  | Suc: "bin_nth w (Suc n) = bin_nth (bin_rest w) n"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   144
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   145
lemma bin_rl_simps [simp]:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   146
  "bin_rl Int.Pls = (Int.Pls, (0::bit))"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   147
  "bin_rl Int.Min = (Int.Min, (1::bit))"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   148
  "bin_rl (Int.Bit0 r) = (r, (0::bit))"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   149
  "bin_rl (Int.Bit1 r) = (r, (1::bit))"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   150
  "bin_rl (r BIT b) = (r, b)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   151
  unfolding bin_rl_char by simp_all
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   152
37546
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   153
lemma bin_rl_simp [simp]:
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   154
  "bin_rest w BIT bin_last w = w"
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   155
  by (simp add: iffD1 [OF bin_rl_char bin_rl_def])
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   156
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   157
lemma bin_abs_lem:
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   158
  "bin = (w BIT b) ==> ~ bin = Int.Min --> ~ bin = Int.Pls -->
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   159
    nat (abs w) < nat (abs bin)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   160
  apply (clarsimp simp add: bin_rl_char)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   161
  apply (unfold Pls_def Min_def Bit_def)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   162
  apply (cases b)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   163
   apply (clarsimp, arith)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   164
  apply (clarsimp, arith)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   165
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   166
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   167
lemma bin_induct:
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   168
  assumes PPls: "P Int.Pls"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   169
    and PMin: "P Int.Min"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   170
    and PBit: "!!bin bit. P bin ==> P (bin BIT bit)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   171
  shows "P bin"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   172
  apply (rule_tac P=P and a=bin and f1="nat o abs" 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   173
                  in wf_measure [THEN wf_induct])
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   174
  apply (simp add: measure_def inv_image_def)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   175
  apply (case_tac x rule: bin_exhaust)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   176
  apply (frule bin_abs_lem)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   177
  apply (auto simp add : PPls PMin PBit)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   178
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   179
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   180
lemma numeral_induct:
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   181
  assumes Pls: "P Int.Pls"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   182
  assumes Min: "P Int.Min"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   183
  assumes Bit0: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Pls\<rbrakk> \<Longrightarrow> P (Int.Bit0 w)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   184
  assumes Bit1: "\<And>w. \<lbrakk>P w; w \<noteq> Int.Min\<rbrakk> \<Longrightarrow> P (Int.Bit1 w)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   185
  shows "P x"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   186
  apply (induct x rule: bin_induct)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   187
    apply (rule Pls)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   188
   apply (rule Min)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   189
  apply (case_tac bit)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   190
   apply (case_tac "bin = Int.Pls")
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   191
    apply simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   192
   apply (simp add: Bit0)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   193
  apply (case_tac "bin = Int.Min")
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   194
   apply simp
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   195
  apply (simp add: Bit1)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   196
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   197
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   198
lemma bin_rest_simps [simp]: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   199
  "bin_rest Int.Pls = Int.Pls"
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   200
  "bin_rest Int.Min = Int.Min"
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   201
  "bin_rest (Int.Bit0 w) = w"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   202
  "bin_rest (Int.Bit1 w) = w"
26514
eff55c0a6d34 tuned towards code generation
haftmann
parents: 26294
diff changeset
   203
  "bin_rest (w BIT b) = w"
37546
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   204
  using bin_rl_simps bin_rl_def by auto
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   205
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   206
lemma bin_last_simps [simp]: 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   207
  "bin_last Int.Pls = (0::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   208
  "bin_last Int.Min = (1::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   209
  "bin_last (Int.Bit0 w) = (0::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   210
  "bin_last (Int.Bit1 w) = (1::bit)"
26514
eff55c0a6d34 tuned towards code generation
haftmann
parents: 26294
diff changeset
   211
  "bin_last (w BIT b) = b"
37546
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   212
  using bin_rl_simps bin_rl_def by auto
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   213
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   214
lemma bin_r_l_extras [simp]:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   215
  "bin_last 0 = (0::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   216
  "bin_last (- 1) = (1::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   217
  "bin_last -1 = (1::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   218
  "bin_last 1 = (1::bit)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   219
  "bin_rest 1 = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   220
  "bin_rest 0 = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   221
  "bin_rest (- 1) = - 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   222
  "bin_rest -1 = -1"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   223
  by (simp_all add: bin_last_def bin_rest_def)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   224
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   225
lemma bin_last_mod: 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   226
  "bin_last w = (if w mod 2 = 0 then (0::bit) else (1::bit))"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   227
  apply (case_tac w rule: bin_exhaust)
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   228
  apply (case_tac b)
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   229
   apply auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   230
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   231
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   232
lemma bin_rest_div: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   233
  "bin_rest w = w div 2"
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   234
  apply (case_tac w rule: bin_exhaust)
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   235
  apply (rule trans)
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   236
   apply clarsimp
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   237
   apply (rule refl)
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   238
  apply (drule trans)
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   239
   apply (rule Bit_def)
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   240
  apply (simp add: z1pdiv2 split: bit.split)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   241
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   242
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   243
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   244
  unfolding bin_rest_div [symmetric] by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   245
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   246
lemma Bit0_div2 [simp]: "(Int.Bit0 w) div 2 = w"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   247
  using Bit_div2 [where 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
   248
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   249
lemma Bit1_div2 [simp]: "(Int.Bit1 w) div 2 = w"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   250
  using Bit_div2 [where 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
   251
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   252
lemma bin_nth_lem [rule_format]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   253
  "ALL y. bin_nth x = bin_nth y --> x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   254
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   255
    apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   256
    apply (erule rev_mp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   257
    apply (induct_tac y rule: bin_induct)
26827
a62f8db42f4a Deleted subset_antisym in a few proofs, because it is
berghofe
parents: 26557
diff changeset
   258
      apply (safe del: subset_antisym)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   259
      apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   260
     apply (erule notE, rule ext, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   261
            drule_tac x="Suc x" in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   262
    apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   263
   apply (erule rev_mp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   264
   apply (induct_tac y rule: bin_induct)
26827
a62f8db42f4a Deleted subset_antisym in a few proofs, because it is
berghofe
parents: 26557
diff changeset
   265
     apply (safe del: subset_antisym)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   266
     apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   267
    apply (erule notE, rule ext, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   268
           drule_tac x="Suc x" in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   269
   apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   270
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   271
  apply clarify
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   272
  apply (erule allE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   273
  apply (erule impE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   274
   prefer 2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   275
   apply (erule BIT_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   276
   apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   277
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   278
  apply (drule_tac x="Suc ?x" in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   279
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   280
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   281
lemma bin_nth_eq_iff: "(bin_nth x = bin_nth y) = (x = y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   282
  by (auto elim: bin_nth_lem)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   283
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   284
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   285
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   286
lemma bin_nth_Pls [simp]: "~ bin_nth Int.Pls n"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   287
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   288
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   289
lemma bin_nth_Min [simp]: "bin_nth Int.Min n"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   290
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   291
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   292
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = (1::bit))"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   293
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   294
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   295
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   296
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   297
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   298
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
   299
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   300
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   301
lemma bin_nth_minus_Bit0 [simp]:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   302
  "0 < n ==> bin_nth (Int.Bit0 w) n = bin_nth w (n - 1)"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   303
  using bin_nth_minus [where 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
   304
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   305
lemma bin_nth_minus_Bit1 [simp]:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   306
  "0 < n ==> bin_nth (Int.Bit1 w) n = bin_nth w (n - 1)"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   307
  using bin_nth_minus [where 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
   308
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   309
lemmas bin_nth_0 = bin_nth.simps(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   310
lemmas bin_nth_Suc = bin_nth.simps(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   311
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   312
lemmas bin_nth_simps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   313
  bin_nth_0 bin_nth_Suc bin_nth_Pls bin_nth_Min bin_nth_minus
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   314
  bin_nth_minus_Bit0 bin_nth_minus_Bit1
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   315
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   316
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   317
subsection {* Recursion combinator for binary integers *}
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   318
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   319
lemma brlem: "(bin = Int.Min) = (- bin + Int.pred 0 = 0)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   320
  unfolding Min_def pred_def by arith
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   321
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   322
function
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   323
  bin_rec :: "'a \<Rightarrow> 'a \<Rightarrow> (int \<Rightarrow> bit \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> int \<Rightarrow> 'a"  
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   324
where 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   325
  "bin_rec f1 f2 f3 bin = (if bin = Int.Pls then f1 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   326
    else if bin = Int.Min then f2
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   327
    else case bin_rl bin of (w, b) => f3 w b (bin_rec f1 f2 f3 w))"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   328
  by pat_completeness auto
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   329
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   330
termination 
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   331
  apply (relation "measure (nat o abs o snd o snd o snd)")
37546
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   332
  apply (auto simp add: bin_rl_def bin_last_def bin_rest_def)
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   333
  unfolding Pls_def Min_def Bit0_def Bit1_def number_of_is_id
d1fa353e1c4a more direct definition simplifies proofs
haftmann
parents: 36176
diff changeset
   334
  apply auto
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   335
  done
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   336
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   337
declare bin_rec.simps [simp del]
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   338
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   339
lemma bin_rec_PM:
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   340
  "f = bin_rec f1 f2 f3 ==> f Int.Pls = f1 & f Int.Min = f2"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   341
  by (auto simp add: bin_rec.simps)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   342
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   343
lemma bin_rec_Pls: "bin_rec f1 f2 f3 Int.Pls = f1"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   344
  by (simp add: bin_rec.simps)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   345
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   346
lemma bin_rec_Min: "bin_rec f1 f2 f3 Int.Min = f2"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   347
  by (simp add: bin_rec.simps)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   348
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   349
lemma bin_rec_Bit0:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   350
  "f3 Int.Pls (0::bit) f1 = f1 \<Longrightarrow>
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   351
    bin_rec f1 f2 f3 (Int.Bit0 w) = f3 w (0::bit) (bin_rec f1 f2 f3 w)"
28959
9d35303719b5 fixed proofs due to changes in Int.thy
huffman
parents: 28562
diff changeset
   352
  by (simp add: bin_rec_Pls bin_rec.simps [of _ _ _ "Int.Bit0 w"])
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   353
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   354
lemma bin_rec_Bit1:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   355
  "f3 Int.Min (1::bit) f2 = f2 \<Longrightarrow>
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   356
    bin_rec f1 f2 f3 (Int.Bit1 w) = f3 w (1::bit) (bin_rec f1 f2 f3 w)"
28959
9d35303719b5 fixed proofs due to changes in Int.thy
huffman
parents: 28562
diff changeset
   357
  by (simp add: bin_rec_Min bin_rec.simps [of _ _ _ "Int.Bit1 w"])
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   358
  
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   359
lemma bin_rec_Bit:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   360
  "f = bin_rec f1 f2 f3  ==> f3 Int.Pls (0::bit) f1 = f1 ==> 
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   361
    f3 Int.Min (1::bit) f2 = f2 ==> f (w BIT b) = f3 w b (f w)"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   362
  by (cases b, simp add: bin_rec_Bit0, simp add: bin_rec_Bit1)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   363
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   364
lemmas bin_rec_simps = refl [THEN bin_rec_Bit] bin_rec_Pls bin_rec_Min
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   365
  bin_rec_Bit0 bin_rec_Bit1
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   366
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   367
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   368
subsection {* Truncating binary integers *}
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   369
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   370
definition
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 26827
diff changeset
   371
  bin_sign_def [code del] : "bin_sign = bin_rec Int.Pls Int.Min (%w b s. s)"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   372
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   373
lemma bin_sign_simps [simp]:
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   374
  "bin_sign Int.Pls = Int.Pls"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   375
  "bin_sign Int.Min = Int.Min"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   376
  "bin_sign (Int.Bit0 w) = bin_sign w"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   377
  "bin_sign (Int.Bit1 w) = bin_sign w"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   378
  "bin_sign (w BIT b) = bin_sign w"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   379
  unfolding bin_sign_def by (auto simp: bin_rec_simps)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   380
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 26827
diff changeset
   381
declare bin_sign_simps(1-4) [code]
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   382
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   383
lemma bin_sign_rest [simp]: 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   384
  "bin_sign (bin_rest w) = (bin_sign w)"
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   385
  by (cases w rule: bin_exhaust) auto
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   386
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   387
consts
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   388
  bintrunc :: "nat => int => int"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   389
primrec 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   390
  Z : "bintrunc 0 bin = Int.Pls"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   391
  Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   392
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   393
consts
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   394
  sbintrunc :: "nat => int => int" 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   395
primrec 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   396
  Z : "sbintrunc 0 bin = 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   397
    (case bin_last bin of (1::bit) => Int.Min | (0::bit) => Int.Pls)"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   398
  Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   399
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   400
lemma sign_bintr:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   401
  "!!w. bin_sign (bintrunc n w) = Int.Pls"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   402
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   403
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   404
lemma bintrunc_mod2p:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   405
  "!!w. bintrunc n w = (w mod 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   406
  apply (induct n, clarsimp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   407
  apply (simp add: bin_last_mod bin_rest_div Bit_def zmod_zmult2_eq
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   408
              cong: number_of_False_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   409
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   410
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   411
lemma sbintrunc_mod2p:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   412
  "!!w. sbintrunc n w = ((w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   413
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   414
   apply clarsimp
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   415
   apply (subst mod_add_left_eq)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   416
   apply (simp add: bin_last_mod)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   417
   apply (simp add: number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   418
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   419
  apply (simp add: bin_last_mod bin_rest_div Bit_def 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   420
              cong: number_of_False_cong)
30940
663af91c0720 zmod_zmult_zmult1 now subsumed by mod_mult_mult1
haftmann
parents: 30034
diff changeset
   421
  apply (clarsimp simp: mod_mult_mult1 [symmetric] 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   422
         zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   423
  apply (rule trans [symmetric, OF _ emep1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   424
     apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   425
  apply (auto simp: even_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   426
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   427
24465
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   428
subsection "Simplifications for (s)bintrunc"
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   429
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   430
lemma bit_bool:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   431
  "(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
   432
  by (cases b') auto
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   433
70f0214b3ecc revert to Word library version from 2007/08/20
huffman
parents: 24419
diff changeset
   434
lemmas bit_bool1 [simp] = refl [THEN bit_bool [THEN iffD1], symmetric]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   435
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   436
lemma bin_sign_lem:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   437
  "!!bin. (bin_sign (sbintrunc n bin) = Int.Min) = bin_nth bin n"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   438
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   439
   apply (case_tac bin rule: bin_exhaust, case_tac b, auto)+
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   440
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   441
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   442
lemma nth_bintr:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   443
  "!!w m. bin_nth (bintrunc m w) n = (n < m & bin_nth w n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   444
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   445
   apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   446
  apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   447
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   448
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   449
lemma nth_sbintr:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   450
  "!!w m. bin_nth (sbintrunc m w) n = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   451
          (if n < m then bin_nth w n else bin_nth w m)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   452
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   453
   apply (case_tac m, simp_all split: bit.splits)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   454
  apply (case_tac m, simp_all split: bit.splits)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   455
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   456
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   457
lemma bin_nth_Bit:
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   458
  "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
   459
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   460
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   461
lemma bin_nth_Bit0:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   462
  "bin_nth (Int.Bit0 w) n = (EX m. n = Suc m & bin_nth w m)"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   463
  using bin_nth_Bit [where 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
   464
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   465
lemma bin_nth_Bit1:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   466
  "bin_nth (Int.Bit1 w) n = (n = 0 | (EX m. n = Suc m & bin_nth w m))"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   467
  using bin_nth_Bit [where 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
   468
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   469
lemma bintrunc_bintrunc_l:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   470
  "n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   471
  by (rule bin_eqI) (auto simp add : nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   472
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   473
lemma sbintrunc_sbintrunc_l:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   474
  "n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)"
32439
7a91c7bcfe7e tuned proofs
nipkow
parents: 30971
diff changeset
   475
  by (rule bin_eqI) (auto simp: nth_sbintr)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   476
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   477
lemma bintrunc_bintrunc_ge:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   478
  "n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   479
  by (rule bin_eqI) (auto simp: nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   480
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   481
lemma bintrunc_bintrunc_min [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   482
  "bintrunc m (bintrunc n w) = bintrunc (min m n) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   483
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   484
  apply (auto simp: nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   485
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   486
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   487
lemma sbintrunc_sbintrunc_min [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   488
  "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   489
  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
   490
  apply (auto simp: nth_sbintr min_max.inf_absorb1 min_max.inf_absorb2)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   491
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   492
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   493
lemmas bintrunc_Pls = 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   494
  bintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   495
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   496
lemmas bintrunc_Min [simp] = 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   497
  bintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   498
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   499
lemmas bintrunc_BIT  [simp] = 
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   500
  bintrunc.Suc [where bin="w BIT b", simplified bin_last_simps bin_rest_simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   501
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   502
lemma bintrunc_Bit0 [simp]:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   503
  "bintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (bintrunc n w)"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   504
  using bintrunc_BIT [where 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
   505
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   506
lemma bintrunc_Bit1 [simp]:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   507
  "bintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (bintrunc n w)"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   508
  using bintrunc_BIT [where 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
   509
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   510
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   511
  bintrunc_Bit0 bintrunc_Bit1
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   512
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   513
lemmas sbintrunc_Suc_Pls = 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   514
  sbintrunc.Suc [where bin="Int.Pls", simplified bin_last_simps bin_rest_simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   515
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   516
lemmas sbintrunc_Suc_Min = 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   517
  sbintrunc.Suc [where bin="Int.Min", simplified bin_last_simps bin_rest_simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   518
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   519
lemmas sbintrunc_Suc_BIT [simp] = 
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   520
  sbintrunc.Suc [where bin="w BIT b", simplified bin_last_simps bin_rest_simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   521
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   522
lemma sbintrunc_Suc_Bit0 [simp]:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   523
  "sbintrunc (Suc n) (Int.Bit0 w) = Int.Bit0 (sbintrunc n w)"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   524
  using sbintrunc_Suc_BIT [where 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
   525
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   526
lemma sbintrunc_Suc_Bit1 [simp]:
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   527
  "sbintrunc (Suc n) (Int.Bit1 w) = Int.Bit1 (sbintrunc n w)"
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   528
  using sbintrunc_Suc_BIT [where 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
   529
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   530
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   531
  sbintrunc_Suc_Bit0 sbintrunc_Suc_Bit1
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   532
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   533
lemmas sbintrunc_Pls = 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   534
  sbintrunc.Z [where bin="Int.Pls", 
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   535
               simplified bin_last_simps bin_rest_simps bit.simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   536
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   537
lemmas sbintrunc_Min = 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   538
  sbintrunc.Z [where bin="Int.Min", 
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   539
               simplified bin_last_simps bin_rest_simps bit.simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   540
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   541
lemmas sbintrunc_0_BIT_B0 [simp] = 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   542
  sbintrunc.Z [where bin="w BIT (0::bit)", 
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   543
               simplified bin_last_simps bin_rest_simps bit.simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   544
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   545
lemmas sbintrunc_0_BIT_B1 [simp] = 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   546
  sbintrunc.Z [where bin="w BIT (1::bit)", 
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   547
               simplified bin_last_simps bin_rest_simps bit.simps, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   548
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   549
lemma sbintrunc_0_Bit0 [simp]: "sbintrunc 0 (Int.Bit0 w) = Int.Pls"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   550
  using sbintrunc_0_BIT_B0 by simp
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   551
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   552
lemma sbintrunc_0_Bit1 [simp]: "sbintrunc 0 (Int.Bit1 w) = Int.Min"
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   553
  using sbintrunc_0_BIT_B1 by simp
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   554
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   555
lemmas sbintrunc_0_simps =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   556
  sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   557
  sbintrunc_0_Bit0 sbintrunc_0_Bit1
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   558
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   559
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   560
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   561
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   562
lemma bintrunc_minus:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   563
  "0 < n ==> bintrunc (Suc (n - 1)) w = bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   564
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   565
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   566
lemma sbintrunc_minus:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   567
  "0 < n ==> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   568
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   569
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   570
lemmas bintrunc_minus_simps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   571
  bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   572
lemmas sbintrunc_minus_simps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   573
  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   574
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   575
lemma bintrunc_n_Pls [simp]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   576
  "bintrunc n Int.Pls = Int.Pls"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   577
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   578
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   579
lemma sbintrunc_n_PM [simp]:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   580
  "sbintrunc n Int.Pls = Int.Pls"
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   581
  "sbintrunc n Int.Min = Int.Min"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   582
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   583
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   584
lemmas thobini1 = arg_cong [where f = "%w. w BIT b", standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   585
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   586
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   587
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   588
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   589
lemmas bmsts = bintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans], standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   590
lemmas bintrunc_Pls_minus_I = bmsts(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   591
lemmas bintrunc_Min_minus_I = bmsts(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   592
lemmas bintrunc_BIT_minus_I = bmsts(3)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   593
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   594
lemma bintrunc_0_Min: "bintrunc 0 Int.Min = Int.Pls"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   595
  by auto
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   596
lemma bintrunc_0_BIT: "bintrunc 0 (w BIT b) = Int.Pls"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   597
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   598
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   599
lemma bintrunc_Suc_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   600
  "bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   601
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   602
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   603
lemmas bintrunc_Suc_Ialts = 
26294
c5fe289de634 fixed broken bintrunc lemma
kleing
parents: 26086
diff changeset
   604
  bintrunc_Min_I [THEN bintrunc_Suc_lem, standard]
c5fe289de634 fixed broken bintrunc lemma
kleing
parents: 26086
diff changeset
   605
  bintrunc_BIT_I [THEN bintrunc_Suc_lem, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   606
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   607
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   608
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   609
lemmas sbintrunc_Suc_Is = 
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   610
  sbintrunc_Sucs(1-3) [THEN thobini1 [THEN [2] trans], standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   611
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   612
lemmas sbintrunc_Suc_minus_Is = 
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   613
  sbintrunc_minus_simps(1-3) [THEN thobini1 [THEN [2] trans], standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   614
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   615
lemma sbintrunc_Suc_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   616
  "sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   617
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   618
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   619
lemmas sbintrunc_Suc_Ialts = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   620
  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   621
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   622
lemma sbintrunc_bintrunc_lt:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   623
  "m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   624
  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   625
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   626
lemma bintrunc_sbintrunc_le:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   627
  "m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   628
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   629
  apply (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   630
   apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   631
  apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   632
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   633
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   634
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   635
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   636
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   637
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   638
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   639
lemma bintrunc_sbintrunc' [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   640
  "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   641
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   642
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   643
lemma sbintrunc_bintrunc' [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   644
  "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   645
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   646
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   647
lemma bin_sbin_eq_iff: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   648
  "bintrunc (Suc n) x = bintrunc (Suc n) y <-> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   649
   sbintrunc n x = sbintrunc n y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   650
  apply (rule iffI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   651
   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   652
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   653
  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   654
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   655
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   656
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   657
lemma bin_sbin_eq_iff':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   658
  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <-> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   659
            sbintrunc (n - 1) x = sbintrunc (n - 1) y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   660
  by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   661
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   662
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   663
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   664
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   665
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   666
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   667
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   668
(* although bintrunc_minus_simps, if added to default simpset,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   669
  tends to get applied where it's not wanted in developing the theories,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   670
  we get a version for when the word length is given literally *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   671
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   672
lemmas nat_non0_gr = 
25134
3d4953e88449 Eliminated most of the neq0_conv occurrences. As a result, many
nipkow
parents: 24465
diff changeset
   673
  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] refl, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   674
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   675
lemmas bintrunc_pred_simps [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   676
  bintrunc_minus_simps [of "number_of bin", simplified nobm1, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   677
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   678
lemmas sbintrunc_pred_simps [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   679
  sbintrunc_minus_simps [of "number_of bin", simplified nobm1, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   680
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   681
lemma no_bintr_alt:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   682
  "number_of (bintrunc n w) = w mod 2 ^ n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   683
  by (simp add: number_of_eq bintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   684
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   685
lemma no_bintr_alt1: "bintrunc n = (%w. w mod 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   686
  by (rule ext) (rule bintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   687
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   688
lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   689
  apply (unfold no_bintr_alt1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   690
  apply (auto simp add: image_iff)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   691
  apply (rule exI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   692
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   693
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   694
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   695
lemma no_bintr: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   696
  "number_of (bintrunc n w) = (number_of w mod 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   697
  by (simp add : bintrunc_mod2p number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   698
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   699
lemma no_sbintr_alt2: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   700
  "sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   701
  by (rule ext) (simp add : sbintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   702
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   703
lemma no_sbintr: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   704
  "number_of (sbintrunc n w) = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   705
   ((number_of w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   706
  by (simp add : no_sbintr_alt2 number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   707
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   708
lemma range_sbintrunc: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   709
  "range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   710
  apply (unfold no_sbintr_alt2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   711
  apply (auto simp add: image_iff eq_diff_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   712
  apply (rule exI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   713
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   714
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   715
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   716
lemma sb_inc_lem:
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   717
  "(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
   718
  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
   719
  apply (rule TrueI)
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   720
  done
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   721
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   722
lemma sb_inc_lem':
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   723
  "(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
   724
  by (rule sb_inc_lem) simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   725
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   726
lemma sbintrunc_inc:
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   727
  "x < - (2^n) ==> x + 2^(Suc n) <= sbintrunc n x"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   728
  unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   729
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   730
lemma sb_dec_lem:
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   731
  "(0::int) <= - (2^k) + a ==> (a + 2^k) mod (2 * 2 ^ k) <= - (2 ^ k) + a"
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   732
  by (rule int_mod_le' [where n = "2 ^ (Suc k)" and b = "a + 2 ^ k",
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   733
    simplified zless2p, OF _ TrueI, simplified])
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   734
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   735
lemma sb_dec_lem':
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   736
  "(2::int) ^ k <= a ==> (a + 2 ^ k) mod (2 * 2 ^ k) <= - (2 ^ k) + a"
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   737
  by (rule iffD1 [OF diff_le_eq', THEN sb_dec_lem, simplified])
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   738
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   739
lemma sbintrunc_dec:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   740
  "x >= (2 ^ n) ==> x - 2 ^ (Suc n) >= sbintrunc n x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   741
  unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   742
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   743
lemmas zmod_uminus' = zmod_uminus [where b="c", standard]
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   744
lemmas zpower_zmod' = zpower_zmod [where m="c" and y="k", standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   745
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   746
lemmas brdmod1s' [symmetric] = 
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   747
  mod_add_left_eq mod_add_right_eq 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   748
  zmod_zsub_left_eq zmod_zsub_right_eq 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   749
  zmod_zmult1_eq zmod_zmult1_eq_rev 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   750
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   751
lemmas brdmods' [symmetric] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   752
  zpower_zmod' [symmetric]
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   753
  trans [OF mod_add_left_eq mod_add_right_eq] 
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   754
  trans [OF zmod_zsub_left_eq zmod_zsub_right_eq] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   755
  trans [OF zmod_zmult1_eq zmod_zmult1_eq_rev] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   756
  zmod_uminus' [symmetric]
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   757
  mod_add_left_eq [where b = "1::int"]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   758
  zmod_zsub_left_eq [where b = "1"]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   759
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   760
lemmas bintr_arith1s =
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   761
  brdmod1s' [where c="2^n::int", folded pred_def succ_def bintrunc_mod2p, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   762
lemmas bintr_ariths =
30034
60f64f112174 removed redundant thms
nipkow
parents: 29631
diff changeset
   763
  brdmods' [where c="2^n::int", folded pred_def succ_def bintrunc_mod2p, standard]
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   764
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   765
lemmas m2pths = pos_mod_sign pos_mod_bound [OF zless2p, standard] 
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   766
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   767
lemma bintr_ge0: "(0 :: int) <= number_of (bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   768
  by (simp add : no_bintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   769
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   770
lemma bintr_lt2p: "number_of (bintrunc n w) < (2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   771
  by (simp add : no_bintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   772
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   773
lemma bintr_Min: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   774
  "number_of (bintrunc n Int.Min) = (2 ^ n :: int) - 1"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   775
  by (simp add : no_bintr m1mod2k)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   776
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   777
lemma sbintr_ge: "(- (2 ^ n) :: int) <= number_of (sbintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   778
  by (simp add : no_sbintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   779
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   780
lemma sbintr_lt: "number_of (sbintrunc n w) < (2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   781
  by (simp add : no_sbintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   782
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   783
lemma bintrunc_Suc:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   784
  "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT bin_last bin"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   785
  by (case_tac bin rule: bin_exhaust) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   786
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   787
lemma sign_Pls_ge_0: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   788
  "(bin_sign bin = Int.Pls) = (number_of bin >= (0 :: int))"
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   789
  by (induct bin rule: numeral_induct) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   790
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   791
lemma sign_Min_lt_0: 
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   792
  "(bin_sign bin = Int.Min) = (number_of bin < (0 :: int))"
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25919
diff changeset
   793
  by (induct bin rule: numeral_induct) auto
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   794
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   795
lemmas sign_Min_neg = trans [OF sign_Min_lt_0 neg_def [symmetric]] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   796
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   797
lemma bin_rest_trunc:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   798
  "!!bin. (bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   799
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   800
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   801
lemma bin_rest_power_trunc [rule_format] :
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   802
  "(bin_rest ^^ k) (bintrunc n bin) = 
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   803
    bintrunc (n - k) ((bin_rest ^^ k) bin)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   804
  by (induct k) (auto simp: bin_rest_trunc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   805
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   806
lemma bin_rest_trunc_i:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   807
  "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   808
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   809
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   810
lemma bin_rest_strunc:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   811
  "!!bin. bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   812
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   813
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   814
lemma bintrunc_rest [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   815
  "!!bin. bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   816
  apply (induct n, simp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   817
  apply (case_tac bin rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   818
  apply (auto simp: bintrunc_bintrunc_l)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   819
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   820
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   821
lemma sbintrunc_rest [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   822
  "!!bin. sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   823
  apply (induct n, simp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   824
  apply (case_tac bin rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   825
  apply (auto simp: bintrunc_bintrunc_l split: bit.splits)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   826
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   827
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   828
lemma bintrunc_rest':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   829
  "bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   830
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   831
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   832
lemma sbintrunc_rest' :
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   833
  "sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   834
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   835
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   836
lemma rco_lem:
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   837
  "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
   838
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   839
  apply (induct_tac n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   840
   apply (simp_all (no_asm))
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   841
  apply (drule fun_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   842
  apply (unfold o_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   843
  apply (erule trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   844
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   845
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   846
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   847
lemma rco_alt: "(f o g) ^^ n o f = f o (g o f) ^^ n"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   848
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   849
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   850
   apply (simp_all add: o_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   851
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   852
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   853
lemmas rco_bintr = bintrunc_rest' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   854
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   855
lemmas rco_sbintr = sbintrunc_rest' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   856
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   857
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   858
subsection {* Splitting and concatenation *}
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   859
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   860
primrec bin_split :: "nat \<Rightarrow> int \<Rightarrow> int \<times> int" where
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   861
  Z: "bin_split 0 w = (w, Int.Pls)"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   862
  | Suc: "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w)
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   863
        in (w1, w2 BIT bin_last w))"
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   864
26557
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   865
primrec bin_cat :: "int \<Rightarrow> nat \<Rightarrow> int \<Rightarrow> int" where
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   866
  Z: "bin_cat w 0 v = w"
9e7f95903b24 more new primrec
haftmann
parents: 26514
diff changeset
   867
  | 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
   868
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   869
subsection {* Miscellaneous lemmas *}
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   870
30952
7ab2716dd93b power operation on functions with syntax o^; power operation on relations with syntax ^^
haftmann
parents: 30940
diff changeset
   871
lemma funpow_minus_simp:
30971
7fbebf75b3ef funpow and relpow with shared "^^" syntax
haftmann
parents: 30952
diff changeset
   872
  "0 < n \<Longrightarrow> f ^^ n = f \<circ> f ^^ (n - 1)"
30952
7ab2716dd93b power operation on functions with syntax o^; power operation on relations with syntax ^^
haftmann
parents: 30940
diff changeset
   873
  by (cases n) simp_all
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   874
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   875
lemmas funpow_pred_simp [simp] =
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   876
  funpow_minus_simp [of "number_of bin", simplified nobm1, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   877
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   878
lemmas replicate_minus_simp = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   879
  trans [OF gen_minus [where f = "%n. replicate n x"] replicate.replicate_Suc,
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   880
         standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   881
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   882
lemmas replicate_pred_simp [simp] =
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   883
  replicate_minus_simp [of "number_of bin", simplified nobm1, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   884
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   885
lemmas power_Suc_no [simp] = power_Suc [of "number_of a", standard]
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   886
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   887
lemmas power_minus_simp = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   888
  trans [OF gen_minus [where f = "power f"] power_Suc, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   889
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   890
lemmas power_pred_simp = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   891
  power_minus_simp [of "number_of bin", simplified nobm1, standard]
25349
0d46bea01741 eliminated illegal schematic variables in where/of;
wenzelm
parents: 25134
diff changeset
   892
lemmas power_pred_simp_no [simp] = power_pred_simp [where f= "number_of f", standard]
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   893
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   894
lemma list_exhaust_size_gt0:
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   895
  assumes y: "\<And>a list. y = a # list \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   896
  shows "0 < length y \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   897
  apply (cases y, simp)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   898
  apply (rule y)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   899
  apply fastsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   900
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   901
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   902
lemma list_exhaust_size_eq0:
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   903
  assumes y: "y = [] \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   904
  shows "length y = 0 \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   905
  apply (cases y)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   906
   apply (rule y, simp)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   907
  apply simp
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   908
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   909
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   910
lemma size_Cons_lem_eq:
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   911
  "y = xa # list ==> size y = Suc k ==> size list = k"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   912
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   913
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   914
lemma size_Cons_lem_eq_bin:
25919
8b1c0d434824 joined theories IntDef, Numeral, IntArith to theory Int
haftmann
parents: 25349
diff changeset
   915
  "y = xa # list ==> size y = number_of (Int.succ k) ==> 
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   916
    size list = number_of k"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   917
  by (auto simp: pred_def succ_def split add : split_if_asm)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   918
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   919
lemmas ls_splits = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   920
  prod.split split_split prod.split_asm split_split_asm split_if_asm
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   921
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   922
lemma not_B1_is_B0: "y \<noteq> (1::bit) \<Longrightarrow> y = (0::bit)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   923
  by (cases y) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   924
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   925
lemma B1_ass_B0: 
37654
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   926
  assumes y: "y = (0::bit) \<Longrightarrow> y = (1::bit)"
8e33b9d04a82 use existing bit type from theory Bit
haftmann
parents: 37546
diff changeset
   927
  shows "y = (1::bit)"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   928
  apply (rule classical)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   929
  apply (drule not_B1_is_B0)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   930
  apply (erule y)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   931
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   932
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   933
-- "simplifications for specific word lengths"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   934
lemmas n2s_ths [THEN eq_reflection] = add_2_eq_Suc add_2_eq_Suc'
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   935
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   936
lemmas s2n_ths = n2s_ths [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   937
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   938
end