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