7 in particular, bin_rec and related work |
7 in particular, bin_rec and related work |
8 *) |
8 *) |
9 |
9 |
10 header {* Basic Definitions for Binary Integers *} |
10 header {* Basic Definitions for Binary Integers *} |
11 |
11 |
12 theory BinGeneral imports Num_Lemmas |
12 theory BinGeneral imports BinInduct Num_Lemmas |
13 |
13 |
14 begin |
14 begin |
15 |
15 |
16 subsection {* BIT as a datatype constructor *} |
16 subsection {* BIT as a datatype constructor *} |
17 |
17 |
18 constdefs |
|
19 -- "alternative way of defining @{text bin_last}, @{text bin_rest}" |
|
20 bin_rl :: "int => int * bit" |
|
21 "bin_rl w == SOME (r, l). w = r BIT l" |
|
22 |
|
23 (** ways in which type Bin resembles a datatype **) |
18 (** ways in which type Bin resembles a datatype **) |
24 |
19 |
25 lemma BIT_eq: "u BIT b = v BIT c ==> u = v & b = c" |
20 lemmas BIT_eqE = BIT_eq [THEN conjE, standard] |
26 apply (unfold Bit_def) |
21 |
27 apply (simp (no_asm_use) split: bit.split_asm) |
22 lemmas BIT_eqI = conjI [THEN BIT_eq_iff [THEN iffD2]] |
28 apply simp_all |
|
29 apply (drule_tac f=even in arg_cong, clarsimp)+ |
|
30 done |
|
31 |
|
32 lemmas BIT_eqE [elim!] = BIT_eq [THEN conjE, standard] |
|
33 |
|
34 lemma BIT_eq_iff [simp]: |
|
35 "(u BIT b = v BIT c) = (u = v \<and> b = c)" |
|
36 by (rule iffI) auto |
|
37 |
|
38 lemmas BIT_eqI [intro!] = conjI [THEN BIT_eq_iff [THEN iffD2]] |
|
39 |
23 |
40 lemma less_Bits: |
24 lemma less_Bits: |
41 "(v BIT b < w BIT c) = (v < w | v <= w & b = bit.B0 & c = bit.B1)" |
25 "(v BIT b < w BIT c) = (v < w | v <= w & b = bit.B0 & c = bit.B1)" |
42 unfolding Bit_def by (auto split: bit.split) |
26 unfolding Bit_def by (auto split: bit.split) |
43 |
27 |
53 apply (cases y rule: bit.exhaust, simp) |
37 apply (cases y rule: bit.exhaust, simp) |
54 apply (simp add: ne) |
38 apply (simp add: ne) |
55 done |
39 done |
56 |
40 |
57 lemma bin_ex_rl: "EX w b. w BIT b = bin" |
41 lemma bin_ex_rl: "EX w b. w BIT b = bin" |
58 apply (unfold Bit_def) |
42 by (insert BIT_exhausts [of bin], auto) |
59 apply (cases "even bin") |
|
60 apply (clarsimp simp: even_equiv_def) |
|
61 apply (auto simp: odd_equiv_def split: bit.split) |
|
62 done |
|
63 |
43 |
64 lemma bin_exhaust: |
44 lemma bin_exhaust: |
65 assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q" |
45 assumes Q: "\<And>x b. bin = x BIT b \<Longrightarrow> Q" |
66 shows "Q" |
46 shows "Q" |
67 apply (insert bin_ex_rl [of bin]) |
47 by (rule BIT_cases, rule Q) |
68 apply (erule exE)+ |
|
69 apply (rule Q) |
|
70 apply force |
|
71 done |
|
72 |
|
73 lemma bin_rl_char: "(bin_rl w = (r, l)) = (r BIT l = w)" |
|
74 apply (unfold bin_rl_def) |
|
75 apply safe |
|
76 apply (cases w rule: bin_exhaust) |
|
77 apply auto |
|
78 done |
|
79 |
|
80 lemmas bin_rl_simps [THEN bin_rl_char [THEN iffD2], standard, simp] = |
|
81 Pls_0_eq Min_1_eq refl |
|
82 |
|
83 lemma bin_abs_lem: |
|
84 "bin = (w BIT b) ==> ~ bin = Numeral.Min --> ~ bin = Numeral.Pls --> |
|
85 nat (abs w) < nat (abs bin)" |
|
86 apply (clarsimp simp add: bin_rl_char) |
|
87 apply (unfold Pls_def Min_def Bit_def) |
|
88 apply (cases b) |
|
89 apply (clarsimp, arith) |
|
90 apply (clarsimp, arith) |
|
91 done |
|
92 |
|
93 lemma bin_induct: |
|
94 assumes PPls: "P Numeral.Pls" |
|
95 and PMin: "P Numeral.Min" |
|
96 and PBit: "!!bin bit. P bin ==> P (bin BIT bit)" |
|
97 shows "P bin" |
|
98 apply (rule_tac P=P and a=bin and f1="nat o abs" |
|
99 in wf_measure [THEN wf_induct]) |
|
100 apply (simp add: measure_def inv_image_def) |
|
101 apply (case_tac x rule: bin_exhaust) |
|
102 apply (frule bin_abs_lem) |
|
103 apply (auto simp add : PPls PMin PBit) |
|
104 done |
|
105 |
48 |
106 subsection {* Recursion combinator for binary integers *} |
49 subsection {* Recursion combinator for binary integers *} |
107 |
|
108 lemma brlem: "(bin = Numeral.Min) = (- bin + Numeral.pred 0 = 0)" |
|
109 unfolding Min_def pred_def by arith |
|
110 |
50 |
111 function |
51 function |
112 bin_rec' :: "int * 'a * 'a * (int => bit => 'a => 'a) => 'a" |
52 bin_rec' :: "int * 'a * 'a * (int => bit => 'a => 'a) => 'a" |
113 where |
53 where |
114 "bin_rec' (bin, f1, f2, f3) = (if bin = Numeral.Pls then f1 |
54 "bin_rec' (bin, f1, f2, f3) = (if bin = Numeral.Pls then f1 |
115 else if bin = Numeral.Min then f2 |
55 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)))" |
56 else f3 (bin_rest bin) (bin_last bin) |
|
57 (bin_rec' (bin_rest bin, f1, f2, f3)))" |
117 by pat_completeness auto |
58 by pat_completeness auto |
118 |
59 |
119 termination |
60 termination |
120 apply (relation "measure (nat o abs o fst)") |
61 by (relation "measure (size o fst)") simp_all |
121 apply simp |
|
122 apply (case_tac bin rule: bin_exhaust) |
|
123 apply (frule bin_abs_lem) |
|
124 apply simp |
|
125 done |
|
126 |
62 |
127 constdefs |
63 constdefs |
128 bin_rec :: "'a => 'a => (int => bit => 'a => 'a) => int => 'a" |
64 bin_rec :: "'a => 'a => (int => bit => 'a => 'a) => int => 'a" |
129 "bin_rec f1 f2 f3 bin == bin_rec' (bin, f1, f2, f3)" |
65 "bin_rec f1 f2 f3 bin == bin_rec' (bin, f1, f2, f3)" |
130 |
66 |
143 f3 Numeral.Min bit.B1 f2 = f2 ==> f (w BIT b) = f3 w b (f w)" |
79 f3 Numeral.Min bit.B1 f2 = f2 ==> f (w BIT b) = f3 w b (f w)" |
144 apply clarify |
80 apply clarify |
145 apply (unfold bin_rec_def) |
81 apply (unfold bin_rec_def) |
146 apply (rule bin_rec'.simps [THEN trans]) |
82 apply (rule bin_rec'.simps [THEN trans]) |
147 apply auto |
83 apply auto |
148 apply (unfold Pls_def Min_def Bit_def) |
|
149 apply (cases b, auto)+ |
|
150 done |
84 done |
151 |
85 |
152 lemmas bin_rec_simps = refl [THEN bin_rec_Bit] bin_rec_Pls bin_rec_Min |
86 lemmas bin_rec_simps = refl [THEN bin_rec_Bit] bin_rec_Pls bin_rec_Min |
153 |
87 |
154 subsection {* Destructors for binary integers *} |
88 subsection {* Sign of binary integers *} |
155 |
89 |
156 consts |
90 consts |
157 -- "corresponding operations analysing bins" |
|
158 bin_last :: "int => bit" |
|
159 bin_rest :: "int => int" |
|
160 bin_sign :: "int => int" |
91 bin_sign :: "int => int" |
161 |
92 |
162 defs |
93 defs |
163 bin_rest_def : "bin_rest w == fst (bin_rl w)" |
|
164 bin_last_def : "bin_last w == snd (bin_rl w)" |
|
165 bin_sign_def : "bin_sign == bin_rec Numeral.Pls Numeral.Min (%w b s. s)" |
94 bin_sign_def : "bin_sign == bin_rec Numeral.Pls Numeral.Min (%w b s. s)" |
166 |
95 |
167 lemma bin_rl: "bin_rl w = (bin_rest w, bin_last w)" |
96 lemmas bin_rest_simps = |
168 unfolding bin_rest_def bin_last_def by auto |
97 bin_rest_Pls bin_rest_Min bin_rest_BIT |
169 |
98 |
170 lemmas bin_rl_simp [simp] = iffD1 [OF bin_rl_char bin_rl] |
99 lemmas bin_last_simps = |
171 |
100 bin_last_Pls bin_last_Min bin_last_BIT |
172 lemma bin_rest_simps [simp]: |
|
173 "bin_rest Numeral.Pls = Numeral.Pls" |
|
174 "bin_rest Numeral.Min = Numeral.Min" |
|
175 "bin_rest (w BIT b) = w" |
|
176 unfolding bin_rest_def by auto |
|
177 |
|
178 lemma bin_last_simps [simp]: |
|
179 "bin_last Numeral.Pls = bit.B0" |
|
180 "bin_last Numeral.Min = bit.B1" |
|
181 "bin_last (w BIT b) = b" |
|
182 unfolding bin_last_def by auto |
|
183 |
101 |
184 lemma bin_sign_simps [simp]: |
102 lemma bin_sign_simps [simp]: |
185 "bin_sign Numeral.Pls = Numeral.Pls" |
103 "bin_sign Numeral.Pls = Numeral.Pls" |
186 "bin_sign Numeral.Min = Numeral.Min" |
104 "bin_sign Numeral.Min = Numeral.Min" |
187 "bin_sign (w BIT b) = bin_sign w" |
105 "bin_sign (w BIT b) = bin_sign w" |
202 apply (auto simp: number_of_eq) |
120 apply (auto simp: number_of_eq) |
203 done |
121 done |
204 |
122 |
205 lemma bin_last_mod: |
123 lemma bin_last_mod: |
206 "bin_last w = (if w mod 2 = 0 then bit.B0 else bit.B1)" |
124 "bin_last w = (if w mod 2 = 0 then bit.B0 else bit.B1)" |
207 apply (case_tac w rule: bin_exhaust) |
125 apply (subgoal_tac "bin_last w = |
208 apply (case_tac b) |
126 (if number_of w mod 2 = (0::int) then bit.B0 else bit.B1)") |
209 apply auto |
127 apply (simp only: number_of_is_id) |
|
128 apply (induct w rule: int_bin_induct, simp_all) |
210 done |
129 done |
211 |
130 |
212 lemma bin_rest_div: |
131 lemma bin_rest_div: |
213 "bin_rest w = w div 2" |
132 "bin_rest w = w div 2" |
214 apply (case_tac w rule: bin_exhaust) |
133 apply (subgoal_tac "number_of (bin_rest w) = number_of w div (2::int)") |
215 apply (rule trans) |
134 apply (simp only: number_of_is_id) |
216 apply clarsimp |
135 apply (induct w rule: int_bin_induct, simp_all) |
217 apply (rule refl) |
|
218 apply (drule trans) |
|
219 apply (rule Bit_def) |
|
220 apply (simp add: z1pdiv2 split: bit.split) |
|
221 done |
136 done |
222 |
137 |
223 lemma Bit_div2 [simp]: "(w BIT b) div 2 = w" |
138 lemma Bit_div2 [simp]: "(w BIT b) div 2 = w" |
224 unfolding bin_rest_div [symmetric] by auto |
139 unfolding bin_rest_div [symmetric] by auto |
225 |
140 |