src/HOL/Word/BinGeneral.thy
author huffman
Fri, 24 Aug 2007 00:37:12 +0200
changeset 24419 737622204802
parent 24417 5c3858b71f80
child 24465 70f0214b3ecc
permissions -rw-r--r--
remove unused lemmas
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
  ID:     $Id$
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     3
  Author: Jeremy Dawson, NICTA
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     4
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     5
  contains basic definition to do with integers
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     6
  expressed using Pls, Min, BIT and important resulting theorems, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     7
  in particular, bin_rec and related work
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     8
*) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
     9
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    10
header {* Basic Definitions for Binary Integers *}
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    11
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    12
theory BinGeneral imports BinInduct Num_Lemmas
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    13
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    14
begin
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    15
24384
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    16
subsection {* BIT as a datatype constructor *}
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    17
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    18
(** ways in which type Bin resembles a datatype **)
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    19
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    20
lemmas BIT_eqI = conjI [THEN BIT_eq_iff [THEN iffD2]]
24384
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    21
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    22
lemma neB1E [elim!]:
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    23
  assumes ne: "y \<noteq> bit.B1"
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    24
  assumes y: "y = bit.B0 \<Longrightarrow> P"
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    25
  shows "P"
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    26
  apply (rule y)
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    27
  apply (cases y rule: bit.exhaust, simp)
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    28
  apply (simp add: ne)
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    29
  done
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    30
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    31
lemma bin_exhaust:
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    32
  assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q"
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    33
  shows "Q"
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    34
  by (rule BIT_cases, rule Q)
24384
0002537695df move BIT datatype stuff from Num_Lemmas to BinGeneral
huffman
parents: 24383
diff changeset
    35
24350
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    36
subsection {* Recursion combinator for binary integers *}
4d74f37c6367 headers for document generation
huffman
parents: 24333
diff changeset
    37
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    38
function
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    39
  bin_rec' :: "int * 'a * 'a * (int => bit => 'a => 'a) => 'a"  
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    40
  where 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    41
  "bin_rec' (bin, f1, f2, f3) = (if bin = Numeral.Pls then f1 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    42
    else if bin = Numeral.Min then f2
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    43
    else f3 (bin_rest bin) (bin_last bin)
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    44
            (bin_rec' (bin_rest bin, f1, f2, f3)))"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    45
  by pat_completeness auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    46
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    47
termination 
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    48
  by (relation "measure (size o fst)") simp_all
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    49
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    50
constdefs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    51
  bin_rec :: "'a => 'a => (int => bit => 'a => 'a) => int => 'a"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    52
  "bin_rec f1 f2 f3 bin == bin_rec' (bin, f1, f2, f3)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    53
24417
5c3858b71f80 remove lemma bin_rec_PM
huffman
parents: 24413
diff changeset
    54
lemma bin_rec_Pls: "bin_rec f1 f2 f3 Numeral.Pls = f1"
5c3858b71f80 remove lemma bin_rec_PM
huffman
parents: 24413
diff changeset
    55
  unfolding bin_rec_def by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    56
24417
5c3858b71f80 remove lemma bin_rec_PM
huffman
parents: 24413
diff changeset
    57
lemma bin_rec_Min: "bin_rec f1 f2 f3 Numeral.Min = f2"
5c3858b71f80 remove lemma bin_rec_PM
huffman
parents: 24413
diff changeset
    58
  unfolding bin_rec_def by simp
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    59
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    60
lemma bin_rec_Bit:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    61
  "f = bin_rec f1 f2 f3  ==> f3 Numeral.Pls bit.B0 f1 = f1 ==> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    62
    f3 Numeral.Min bit.B1 f2 = f2 ==> f (w BIT b) = f3 w b (f w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    63
  apply clarify
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    64
  apply (unfold bin_rec_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    65
  apply (rule bin_rec'.simps [THEN trans])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    66
  apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    67
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    68
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    69
lemmas bin_rec_simps = refl [THEN bin_rec_Bit] bin_rec_Pls bin_rec_Min
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    70
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    71
subsection {* Sign of binary integers *}
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    72
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    73
consts
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    74
  bin_sign :: "int => int"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    75
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    76
defs  
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    77
  bin_sign_def : "bin_sign == bin_rec Numeral.Pls Numeral.Min (%w b s. s)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    78
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    79
lemmas bin_rest_simps =
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    80
  bin_rest_Pls bin_rest_Min bin_rest_BIT
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
    81
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    82
lemmas bin_last_simps =
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
    83
  bin_last_Pls bin_last_Min bin_last_BIT
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    84
    
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    85
lemma bin_sign_simps [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    86
  "bin_sign Numeral.Pls = Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    87
  "bin_sign Numeral.Min = Numeral.Min"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    88
  "bin_sign (w BIT b) = bin_sign w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    89
  unfolding bin_sign_def by (auto simp: bin_rec_simps)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    90
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    91
lemma bin_r_l_extras [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    92
  "bin_last 0 = bit.B0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    93
  "bin_last (- 1) = bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    94
  "bin_last -1 = bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    95
  "bin_last 1 = bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    96
  "bin_rest 1 = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    97
  "bin_rest 0 = 0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    98
  "bin_rest (- 1) = - 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
    99
  "bin_rest -1 = -1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   100
  apply (unfold number_of_Min)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   101
  apply (unfold Pls_def [symmetric] Min_def [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   102
  apply (unfold numeral_1_eq_1 [symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   103
  apply (auto simp: number_of_eq) 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   104
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   105
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   106
lemma bin_last_mod: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   107
  "bin_last w = (if w mod 2 = 0 then bit.B0 else bit.B1)"
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   108
  apply (subgoal_tac "bin_last w =
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   109
         (if number_of w mod 2 = (0::int) then bit.B0 else bit.B1)")
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   110
   apply (simp only: number_of_is_id)
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   111
  apply (induct w rule: int_bin_induct, simp_all)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   112
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   113
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   114
lemma bin_rest_div: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   115
  "bin_rest w = w div 2"
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   116
  apply (subgoal_tac "number_of (bin_rest w) = number_of w div (2::int)")
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   117
   apply (simp only: number_of_is_id)
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   118
  apply (induct w rule: int_bin_induct, simp_all)
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   119
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   120
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   121
lemma Bit_div2 [simp]: "(w BIT b) div 2 = w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   122
  unfolding bin_rest_div [symmetric] by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   123
24409
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   124
subsection {* Testing bit positions *}
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   125
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   126
consts
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   127
  bin_nth :: "int => nat => bool"
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   128
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   129
primrec
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   130
  Z : "bin_nth w 0 = (bin_last w = bit.B1)"
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   131
  Suc : "bin_nth w (Suc n) = bin_nth (bin_rest w) n"
35485c476d9e move bin_nth stuff to its own subsection
huffman
parents: 24384
diff changeset
   132
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   133
lemma bin_nth_lem [rule_format]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   134
  "ALL y. bin_nth x = bin_nth y --> x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   135
  apply (induct x rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   136
    apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   137
    apply (erule rev_mp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   138
    apply (induct_tac y rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   139
      apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   140
      apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   141
     apply (erule notE, rule ext, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   142
            drule_tac x="Suc x" in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   143
    apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   144
   apply (erule rev_mp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   145
   apply (induct_tac y rule: bin_induct)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   146
     apply safe
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   147
     apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   148
    apply (erule notE, rule ext, 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   149
           drule_tac x="Suc x" in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   150
   apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   151
  apply (case_tac y rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   152
  apply clarify
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   153
  apply (erule allE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   154
  apply (erule impE)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   155
   prefer 2
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   156
   apply (erule BIT_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   157
   apply (drule_tac x=0 in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   158
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   159
  apply (drule_tac x="Suc ?x" in fun_cong, force)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   160
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   161
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   162
lemma bin_nth_eq_iff: "(bin_nth x = bin_nth y) = (x = y)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   163
  by (auto elim: bin_nth_lem)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   164
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   165
lemmas bin_eqI = ext [THEN bin_nth_eq_iff [THEN iffD1], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   166
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   167
lemma bin_nth_Pls [simp]: "~ bin_nth Numeral.Pls n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   168
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   169
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   170
lemma bin_nth_Min [simp]: "bin_nth Numeral.Min n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   171
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   172
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   173
lemma bin_nth_0_BIT: "bin_nth (w BIT b) 0 = (b = bit.B1)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   174
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   175
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   176
lemma bin_nth_Suc_BIT: "bin_nth (w BIT b) (Suc n) = bin_nth w n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   177
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   178
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   179
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
   180
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   181
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   182
lemmas bin_nth_0 = bin_nth.simps(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   183
lemmas bin_nth_Suc = bin_nth.simps(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   184
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   185
lemmas bin_nth_simps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   186
  bin_nth_0 bin_nth_Suc bin_nth_Pls bin_nth_Min bin_nth_minus
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   187
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   188
lemma bin_sign_rest [simp]: 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   189
  "bin_sign (bin_rest w) = (bin_sign w)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   190
  by (case_tac w rule: bin_exhaust) auto
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   191
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   192
subsection {* Truncating binary integers *}
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   193
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   194
consts
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   195
  bintrunc :: "nat => int => int"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   196
primrec 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   197
  Z : "bintrunc 0 bin = Numeral.Pls"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   198
  Suc : "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT (bin_last bin)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   199
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   200
consts
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   201
  sbintrunc :: "nat => int => int" 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   202
primrec 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   203
  Z : "sbintrunc 0 bin = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   204
    (case bin_last bin of bit.B1 => Numeral.Min | bit.B0 => Numeral.Pls)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   205
  Suc : "sbintrunc (Suc n) bin = sbintrunc n (bin_rest bin) BIT (bin_last bin)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   206
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   207
lemma sign_bintr:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   208
  "!!w. bin_sign (bintrunc n w) = Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   209
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   210
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   211
lemma bintrunc_mod2p:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   212
  "!!w. bintrunc n w = (w mod 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   213
  apply (induct n, clarsimp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   214
  apply (simp add: bin_last_mod bin_rest_div Bit_def zmod_zmult2_eq
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   215
              cong: number_of_False_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   216
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   217
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   218
lemma sbintrunc_mod2p:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   219
  "!!w. sbintrunc n w = ((w + 2 ^ n) mod 2 ^ (Suc n) - 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   220
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   221
   apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   222
   apply (subst zmod_zadd_left_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   223
   apply (simp add: bin_last_mod)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   224
   apply (simp add: number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   225
  apply clarsimp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   226
  apply (simp add: bin_last_mod bin_rest_div Bit_def 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   227
              cong: number_of_False_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   228
  apply (clarsimp simp: zmod_zmult_zmult1 [symmetric] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   229
         zmod_zdiv_equality [THEN diff_eq_eq [THEN iffD2 [THEN sym]]])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   230
  apply (rule trans [symmetric, OF _ emep1])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   231
     apply auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   232
  apply (auto simp: even_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   233
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   234
24413
5073729e5c12 import BinInduct;
huffman
parents: 24409
diff changeset
   235
text "Simplifications for (s)bintrunc"
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   236
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   237
lemma bin_sign_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   238
  "!!bin. (bin_sign (sbintrunc n bin) = Numeral.Min) = bin_nth bin n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   239
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   240
   apply (case_tac bin rule: bin_exhaust, case_tac b, auto)+
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 nth_bintr:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   244
  "!!w m. bin_nth (bintrunc m w) n = (n < m & bin_nth w n)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   245
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   246
   apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   247
  apply (case_tac m, auto)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   248
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   249
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   250
lemma nth_sbintr:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   251
  "!!w m. bin_nth (sbintrunc m w) n = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   252
          (if n < m then bin_nth w n else bin_nth w m)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   253
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   254
   apply (case_tac m, simp_all split: bit.splits)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   255
  apply (case_tac m, simp_all split: bit.splits)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   256
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   257
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   258
lemma bin_nth_Bit:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   259
  "bin_nth (w BIT b) n = (n = 0 & b = bit.B1 | (EX m. n = Suc m & bin_nth w m))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   260
  by (cases n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   261
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   262
lemma bintrunc_bintrunc_l:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   263
  "n <= m ==> (bintrunc m (bintrunc n w) = bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   264
  by (rule bin_eqI) (auto simp add : nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   265
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   266
lemma sbintrunc_sbintrunc_l:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   267
  "n <= m ==> (sbintrunc m (sbintrunc n w) = sbintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   268
  by (rule bin_eqI) (auto simp: nth_sbintr min_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   269
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   270
lemma bintrunc_bintrunc_ge:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   271
  "n <= m ==> (bintrunc n (bintrunc m w) = bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   272
  by (rule bin_eqI) (auto simp: nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   273
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   274
lemma bintrunc_bintrunc_min [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   275
  "bintrunc m (bintrunc n w) = bintrunc (min m n) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   276
  apply (unfold min_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   277
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   278
  apply (auto simp: nth_bintr)
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 sbintrunc_sbintrunc_min [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   282
  "sbintrunc m (sbintrunc n w) = sbintrunc (min m n) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   283
  apply (unfold min_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   284
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   285
  apply (auto simp: nth_sbintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   286
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   287
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   288
lemmas bintrunc_Pls = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   289
  bintrunc.Suc [where bin="Numeral.Pls", simplified bin_last_simps bin_rest_simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   290
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   291
lemmas bintrunc_Min [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   292
  bintrunc.Suc [where bin="Numeral.Min", simplified bin_last_simps bin_rest_simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   293
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   294
lemmas bintrunc_BIT  [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   295
  bintrunc.Suc [where bin="?w BIT ?b", simplified bin_last_simps bin_rest_simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   296
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   297
lemmas bintrunc_Sucs = bintrunc_Pls bintrunc_Min bintrunc_BIT
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   298
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   299
lemmas sbintrunc_Suc_Pls = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   300
  sbintrunc.Suc [where bin="Numeral.Pls", simplified bin_last_simps bin_rest_simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   301
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   302
lemmas sbintrunc_Suc_Min = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   303
  sbintrunc.Suc [where bin="Numeral.Min", simplified bin_last_simps bin_rest_simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   304
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   305
lemmas sbintrunc_Suc_BIT [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   306
  sbintrunc.Suc [where bin="?w BIT ?b", simplified bin_last_simps bin_rest_simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   307
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   308
lemmas sbintrunc_Sucs = sbintrunc_Suc_Pls sbintrunc_Suc_Min sbintrunc_Suc_BIT
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   309
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   310
lemmas sbintrunc_Pls = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   311
  sbintrunc.Z [where bin="Numeral.Pls", 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   312
               simplified bin_last_simps bin_rest_simps bit.simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   313
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   314
lemmas sbintrunc_Min = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   315
  sbintrunc.Z [where bin="Numeral.Min", 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   316
               simplified bin_last_simps bin_rest_simps bit.simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   317
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   318
lemmas sbintrunc_0_BIT_B0 [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   319
  sbintrunc.Z [where bin="?w BIT bit.B0", 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   320
               simplified bin_last_simps bin_rest_simps bit.simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   321
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   322
lemmas sbintrunc_0_BIT_B1 [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   323
  sbintrunc.Z [where bin="?w BIT bit.B1", 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   324
               simplified bin_last_simps bin_rest_simps bit.simps]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   325
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   326
lemmas sbintrunc_0_simps =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   327
  sbintrunc_Pls sbintrunc_Min sbintrunc_0_BIT_B0 sbintrunc_0_BIT_B1
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   328
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   329
lemmas bintrunc_simps = bintrunc.Z bintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   330
lemmas sbintrunc_simps = sbintrunc_0_simps sbintrunc_Sucs
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   331
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   332
lemma bintrunc_minus:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   333
  "0 < n ==> bintrunc (Suc (n - 1)) w = bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   334
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   335
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   336
lemma sbintrunc_minus:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   337
  "0 < n ==> sbintrunc (Suc (n - 1)) w = sbintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   338
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   339
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   340
lemmas bintrunc_minus_simps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   341
  bintrunc_Sucs [THEN [2] bintrunc_minus [symmetric, THEN trans], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   342
lemmas sbintrunc_minus_simps = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   343
  sbintrunc_Sucs [THEN [2] sbintrunc_minus [symmetric, THEN trans], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   344
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   345
lemma bintrunc_n_Pls [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   346
  "bintrunc n Numeral.Pls = Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   347
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   348
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   349
lemma sbintrunc_n_PM [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   350
  "sbintrunc n Numeral.Pls = Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   351
  "sbintrunc n Numeral.Min = Numeral.Min"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   352
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   353
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   354
lemmas thobini1 = arg_cong [where f = "%w. w BIT ?b"]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   355
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   356
lemmas bintrunc_BIT_I = trans [OF bintrunc_BIT thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   357
lemmas bintrunc_Min_I = trans [OF bintrunc_Min thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   358
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   359
lemmas bmsts = bintrunc_minus_simps [THEN thobini1 [THEN [2] trans], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   360
lemmas bintrunc_Pls_minus_I = bmsts(1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   361
lemmas bintrunc_Min_minus_I = bmsts(2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   362
lemmas bintrunc_BIT_minus_I = bmsts(3)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   363
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   364
lemma bintrunc_0_Min: "bintrunc 0 Numeral.Min = Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   365
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   366
lemma bintrunc_0_BIT: "bintrunc 0 (w BIT b) = Numeral.Pls"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   367
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   368
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   369
lemma bintrunc_Suc_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   370
  "bintrunc (Suc n) x = y ==> m = Suc n ==> bintrunc m x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   371
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   372
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   373
lemmas bintrunc_Suc_Ialts = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   374
  bintrunc_Min_I bintrunc_BIT_I [THEN bintrunc_Suc_lem, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   375
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   376
lemmas sbintrunc_BIT_I = trans [OF sbintrunc_Suc_BIT thobini1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   377
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   378
lemmas sbintrunc_Suc_Is = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   379
  sbintrunc_Sucs [THEN thobini1 [THEN [2] trans], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   380
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   381
lemmas sbintrunc_Suc_minus_Is = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   382
  sbintrunc_minus_simps [THEN thobini1 [THEN [2] trans], standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   383
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   384
lemma sbintrunc_Suc_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   385
  "sbintrunc (Suc n) x = y ==> m = Suc n ==> sbintrunc m x = y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   386
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   387
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   388
lemmas sbintrunc_Suc_Ialts = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   389
  sbintrunc_Suc_Is [THEN sbintrunc_Suc_lem, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   390
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   391
lemma sbintrunc_bintrunc_lt:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   392
  "m > n ==> sbintrunc n (bintrunc m w) = sbintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   393
  by (rule bin_eqI) (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   394
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   395
lemma bintrunc_sbintrunc_le:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   396
  "m <= Suc n ==> bintrunc m (sbintrunc n w) = bintrunc m w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   397
  apply (rule bin_eqI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   398
  apply (auto simp: nth_sbintr nth_bintr)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   399
   apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   400
  apply (subgoal_tac "x=n", safe, arith+)[1]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   401
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   402
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   403
lemmas bintrunc_sbintrunc [simp] = order_refl [THEN bintrunc_sbintrunc_le]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   404
lemmas sbintrunc_bintrunc [simp] = lessI [THEN sbintrunc_bintrunc_lt]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   405
lemmas bintrunc_bintrunc [simp] = order_refl [THEN bintrunc_bintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   406
lemmas sbintrunc_sbintrunc [simp] = order_refl [THEN sbintrunc_sbintrunc_l] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   407
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   408
lemma bintrunc_sbintrunc' [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   409
  "0 < n \<Longrightarrow> bintrunc n (sbintrunc (n - 1) w) = bintrunc n w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   410
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   411
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   412
lemma sbintrunc_bintrunc' [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   413
  "0 < n \<Longrightarrow> sbintrunc (n - 1) (bintrunc n w) = sbintrunc (n - 1) w"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   414
  by (cases n) (auto simp del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   415
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   416
lemma bin_sbin_eq_iff: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   417
  "bintrunc (Suc n) x = bintrunc (Suc n) y <-> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   418
   sbintrunc n x = sbintrunc n y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   419
  apply (rule iffI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   420
   apply (rule box_equals [OF _ sbintrunc_bintrunc sbintrunc_bintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   421
   apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   422
  apply (rule box_equals [OF _ bintrunc_sbintrunc bintrunc_sbintrunc])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   423
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   424
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   425
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   426
lemma bin_sbin_eq_iff':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   427
  "0 < n \<Longrightarrow> bintrunc n x = bintrunc n y <-> 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   428
            sbintrunc (n - 1) x = sbintrunc (n - 1) y"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   429
  by (cases n) (simp_all add: bin_sbin_eq_iff del: bintrunc.Suc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   430
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   431
lemmas bintrunc_sbintruncS0 [simp] = bintrunc_sbintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   432
lemmas sbintrunc_bintruncS0 [simp] = sbintrunc_bintrunc' [unfolded One_nat_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   433
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   434
lemmas bintrunc_bintrunc_l' = le_add1 [THEN bintrunc_bintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   435
lemmas sbintrunc_sbintrunc_l' = le_add1 [THEN sbintrunc_sbintrunc_l]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   436
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   437
(* although bintrunc_minus_simps, if added to default simpset,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   438
  tends to get applied where it's not wanted in developing the theories,
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   439
  we get a version for when the word length is given literally *)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   440
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   441
lemmas nat_non0_gr = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   442
  trans [OF iszero_def [THEN Not_eq_iff [THEN iffD2]] neq0_conv, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   443
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   444
lemmas bintrunc_pred_simps [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   445
  bintrunc_minus_simps [of "number_of bin", simplified nobm1, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   446
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   447
lemmas sbintrunc_pred_simps [simp] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   448
  sbintrunc_minus_simps [of "number_of bin", simplified nobm1, standard]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   449
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   450
lemma no_bintr_alt:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   451
  "number_of (bintrunc n w) = w mod 2 ^ n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   452
  by (simp add: number_of_eq bintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   453
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   454
lemma no_bintr_alt1: "bintrunc n = (%w. w mod 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   455
  by (rule ext) (rule bintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   456
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   457
lemma range_bintrunc: "range (bintrunc n) = {i. 0 <= i & i < 2 ^ n}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   458
  apply (unfold no_bintr_alt1)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   459
  apply (auto simp add: image_iff)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   460
  apply (rule exI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   461
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   462
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   463
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   464
lemma no_bintr: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   465
  "number_of (bintrunc n w) = (number_of w mod 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   466
  by (simp add : bintrunc_mod2p number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   467
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   468
lemma no_sbintr_alt2: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   469
  "sbintrunc n = (%w. (w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   470
  by (rule ext) (simp add : sbintrunc_mod2p)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   471
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   472
lemma no_sbintr: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   473
  "number_of (sbintrunc n w) = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   474
   ((number_of w + 2 ^ n) mod 2 ^ Suc n - 2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   475
  by (simp add : no_sbintr_alt2 number_of_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   476
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   477
lemma range_sbintrunc: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   478
  "range (sbintrunc n) = {i. - (2 ^ n) <= i & i < 2 ^ n}"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   479
  apply (unfold no_sbintr_alt2)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   480
  apply (auto simp add: image_iff eq_diff_eq)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   481
  apply (rule exI)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   482
  apply (auto intro: int_mod_lem [THEN iffD1, symmetric])
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   483
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   484
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   485
lemmas sb_inc_lem = int_mod_ge' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   486
  [where n = "2 ^ (Suc ?k)" and b = "?a + 2 ^ ?k", 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   487
   simplified zless2p, OF _ TrueI]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   488
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   489
lemmas sb_inc_lem' = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   490
  iffD1 [OF less_diff_eq, THEN sb_inc_lem, simplified OrderedGroup.diff_0]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   491
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   492
lemma sbintrunc_inc:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   493
  "x < - (2 ^ n) ==> x + 2 ^ (Suc n) <= sbintrunc n x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   494
  unfolding no_sbintr_alt2 by (drule sb_inc_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   495
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   496
lemmas sb_dec_lem = int_mod_le' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   497
  [where n = "2 ^ (Suc ?k)" and b = "?a + 2 ^ ?k", 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   498
  simplified zless2p, OF _ TrueI, simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   499
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   500
lemmas sb_dec_lem' = iffD1 [OF diff_le_eq', THEN sb_dec_lem, simplified]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   501
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   502
lemma sbintrunc_dec:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   503
  "x >= (2 ^ n) ==> x - 2 ^ (Suc n) >= sbintrunc n x"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   504
  unfolding no_sbintr_alt2 by (drule sb_dec_lem') simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   505
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   506
lemmas zmod_uminus' = zmod_uminus [where b="?c"]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   507
lemmas zpower_zmod' = zpower_zmod [where m="?c" and y="?k"]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   508
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   509
lemmas brdmod1s' [symmetric] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   510
  zmod_zadd_left_eq zmod_zadd_right_eq 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   511
  zmod_zsub_left_eq zmod_zsub_right_eq 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   512
  zmod_zmult1_eq zmod_zmult1_eq_rev 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   513
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   514
lemmas brdmods' [symmetric] = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   515
  zpower_zmod' [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   516
  trans [OF zmod_zadd_left_eq zmod_zadd_right_eq] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   517
  trans [OF zmod_zsub_left_eq zmod_zsub_right_eq] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   518
  trans [OF zmod_zmult1_eq zmod_zmult1_eq_rev] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   519
  zmod_uminus' [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   520
  zmod_zadd_left_eq [where b = "1"]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   521
  zmod_zsub_left_eq [where b = "1"]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   522
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   523
lemmas bintr_arith1s =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   524
  brdmod1s' [where c="2^?n", folded pred_def succ_def bintrunc_mod2p]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   525
lemmas bintr_ariths =
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   526
  brdmods' [where c="2^?n", folded pred_def succ_def bintrunc_mod2p]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   527
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   528
lemmas m2pths [OF zless2p, standard] = pos_mod_sign pos_mod_bound
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   529
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   530
lemma bintr_ge0: "(0 :: int) <= number_of (bintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   531
  by (simp add : no_bintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   532
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   533
lemma bintr_lt2p: "number_of (bintrunc n w) < (2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   534
  by (simp add : no_bintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   535
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   536
lemma bintr_Min: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   537
  "number_of (bintrunc n Numeral.Min) = (2 ^ n :: int) - 1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   538
  by (simp add : no_bintr m1mod2k)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   539
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   540
lemma sbintr_ge: "(- (2 ^ n) :: int) <= number_of (sbintrunc n w)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   541
  by (simp add : no_sbintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   542
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   543
lemma sbintr_lt: "number_of (sbintrunc n w) < (2 ^ n :: int)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   544
  by (simp add : no_sbintr m2pths)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   545
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   546
lemma bintrunc_Suc:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   547
  "bintrunc (Suc n) bin = bintrunc n (bin_rest bin) BIT bin_last bin"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   548
  by (case_tac bin rule: bin_exhaust) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   549
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   550
lemma sign_Pls_ge_0: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   551
  "(bin_sign bin = Numeral.Pls) = (number_of bin >= (0 :: int))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   552
  by (induct bin rule: bin_induct) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   553
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   554
lemma sign_Min_lt_0: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   555
  "(bin_sign bin = Numeral.Min) = (number_of bin < (0 :: int))"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   556
  by (induct bin rule: bin_induct) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   557
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   558
lemmas sign_Min_neg = trans [OF sign_Min_lt_0 neg_def [symmetric]] 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   559
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   560
lemma bin_rest_trunc:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   561
  "!!bin. (bin_rest (bintrunc n bin)) = bintrunc (n - 1) (bin_rest bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   562
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   563
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   564
lemma bin_rest_power_trunc [rule_format] :
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   565
  "(bin_rest ^ k) (bintrunc n bin) = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   566
    bintrunc (n - k) ((bin_rest ^ k) bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   567
  by (induct k) (auto simp: bin_rest_trunc)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   568
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   569
lemma bin_rest_trunc_i:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   570
  "bintrunc n (bin_rest bin) = bin_rest (bintrunc (Suc n) bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   571
  by auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   572
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   573
lemma bin_rest_strunc:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   574
  "!!bin. bin_rest (sbintrunc (Suc n) bin) = sbintrunc n (bin_rest bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   575
  by (induct n) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   576
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   577
lemma bintrunc_rest [simp]: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   578
  "!!bin. bintrunc n (bin_rest (bintrunc n bin)) = bin_rest (bintrunc n bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   579
  apply (induct n, simp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   580
  apply (case_tac bin rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   581
  apply (auto simp: bintrunc_bintrunc_l)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   582
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   583
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   584
lemma sbintrunc_rest [simp]:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   585
  "!!bin. sbintrunc n (bin_rest (sbintrunc n bin)) = bin_rest (sbintrunc n bin)"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   586
  apply (induct n, simp)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   587
  apply (case_tac bin rule: bin_exhaust)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   588
  apply (auto simp: bintrunc_bintrunc_l split: bit.splits)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   589
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   590
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   591
lemma bintrunc_rest':
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   592
  "bintrunc n o bin_rest o bintrunc n = bin_rest o bintrunc n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   593
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   594
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   595
lemma sbintrunc_rest' :
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   596
  "sbintrunc n o bin_rest o sbintrunc n = bin_rest o sbintrunc n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   597
  by (rule ext) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   598
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   599
lemma rco_lem:
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   600
  "f o g o f = g o f ==> f o (g o f) ^ n = g ^ n o f"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   601
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   602
  apply (induct_tac n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   603
   apply (simp_all (no_asm))
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   604
  apply (drule fun_cong)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   605
  apply (unfold o_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   606
  apply (erule trans)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   607
  apply simp
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   608
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   609
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   610
lemma rco_alt: "(f o g) ^ n o f = f o (g o f) ^ n"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   611
  apply (rule ext)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   612
  apply (induct n)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   613
   apply (simp_all add: o_def)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   614
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   615
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   616
lemmas rco_bintr = bintrunc_rest' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   617
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   618
lemmas rco_sbintr = sbintrunc_rest' 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   619
  [THEN rco_lem [THEN fun_cong], unfolded o_def]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   620
24364
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   621
subsection {* Splitting and concatenation *}
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   622
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   623
consts
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   624
  bin_split :: "nat => int => int * int"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   625
primrec
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   626
  Z : "bin_split 0 w = (w, Numeral.Pls)"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   627
  Suc : "bin_split (Suc n) w = (let (w1, w2) = bin_split n (bin_rest w)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   628
    in (w1, w2 BIT bin_last w))"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   629
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   630
consts
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   631
  bin_cat :: "int => nat => int => int"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   632
primrec
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   633
  Z : "bin_cat w 0 v = w"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   634
  Suc : "bin_cat w (Suc n) v = bin_cat w n (bin_rest v) BIT bin_last v"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   635
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   636
subsection {* Miscellaneous lemmas *}
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   637
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   638
lemmas funpow_minus_simp = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   639
  trans [OF gen_minus [where f = "power f"] funpow_Suc, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   640
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   641
lemmas funpow_pred_simp [simp] =
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   642
  funpow_minus_simp [of "number_of bin", simplified nobm1, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   643
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   644
lemmas replicate_minus_simp = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   645
  trans [OF gen_minus [where f = "%n. replicate n x"] replicate.replicate_Suc,
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   646
         standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   647
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   648
lemmas replicate_pred_simp [simp] =
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   649
  replicate_minus_simp [of "number_of bin", simplified nobm1, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   650
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   651
lemmas power_Suc_no [simp] = power_Suc [of "number_of ?a"]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   652
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   653
lemmas power_minus_simp = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   654
  trans [OF gen_minus [where f = "power f"] power_Suc, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   655
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   656
lemmas power_pred_simp = 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   657
  power_minus_simp [of "number_of bin", simplified nobm1, standard]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   658
lemmas power_pred_simp_no [simp] = power_pred_simp [where f= "number_of ?f"]
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   659
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   660
lemma list_exhaust_size_gt0:
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   661
  assumes y: "\<And>a list. y = a # list \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   662
  shows "0 < length y \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   663
  apply (cases y, simp)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   664
  apply (rule y)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   665
  apply fastsimp
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   666
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   667
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   668
lemma list_exhaust_size_eq0:
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   669
  assumes y: "y = [] \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   670
  shows "length y = 0 \<Longrightarrow> P"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   671
  apply (cases y)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   672
   apply (rule y, simp)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   673
  apply simp
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   674
  done
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   675
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   676
lemma size_Cons_lem_eq:
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   677
  "y = xa # list ==> size y = Suc k ==> size list = k"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   678
  by auto
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   679
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   680
lemma size_Cons_lem_eq_bin:
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   681
  "y = xa # list ==> size y = number_of (Numeral.succ k) ==> 
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   682
    size list = number_of k"
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   683
  by (auto simp: pred_def succ_def split add : split_if_asm)
31e359126ab6 reorganize into subsections
huffman
parents: 24350
diff changeset
   684
24333
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   685
lemmas ls_splits = 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   686
  prod.split split_split prod.split_asm split_split_asm split_if_asm
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   687
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   688
lemma not_B1_is_B0: "y \<noteq> bit.B1 \<Longrightarrow> y = bit.B0"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   689
  by (cases y) auto
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   690
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   691
lemma B1_ass_B0: 
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   692
  assumes y: "y = bit.B0 \<Longrightarrow> y = bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   693
  shows "y = bit.B1"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   694
  apply (rule classical)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   695
  apply (drule not_B1_is_B0)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   696
  apply (erule y)
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   697
  done
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   698
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   699
-- "simplifications for specific word lengths"
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   700
lemmas n2s_ths [THEN eq_reflection] = add_2_eq_Suc add_2_eq_Suc'
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   701
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   702
lemmas s2n_ths = n2s_ths [symmetric]
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   703
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   704
e77ea0ea7f2c * HOL-Word:
kleing
parents:
diff changeset
   705
end