]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/ng_assembly/num/byte8.ma
freescale porting
[helm.git] / helm / software / matita / contribs / ng_assembly / num / byte8.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* ********************************************************************** *)
16 (*                          Progetto FreeScale                            *)
17 (*                                                                        *)
18 (*   Sviluppato da: Ing. Cosimo Oliboni, oliboni@cs.unibo.it              *)
19 (*   Sviluppo: 2008-2010                                                  *)
20 (*                                                                        *)
21 (* ********************************************************************** *)
22
23 include "num/exadecim.ma".
24 include "num/comp_num.ma".
25 include "num/bitrigesim.ma".
26 include "common/nat.ma".
27
28 (* **** *)
29 (* BYTE *)
30 (* **** *)
31
32 ndefinition byte8 ≝ comp_num exadecim.
33 ndefinition mk_byte8 ≝ λe1,e2.mk_comp_num exadecim e1 e2.
34
35 (* \langle \rangle *)
36 notation "〈x,y〉" non associative with precedence 80
37  for @{ mk_comp_num exadecim $x $y }.
38
39 (* iteratore sui byte *)
40 ndefinition forall_b8 ≝ forall_cn ? forall_ex.
41
42 (* operatore = *)
43 ndefinition eq_b8 ≝ eq2_cn ? eq_ex.
44
45 (* operatore < *)
46 ndefinition lt_b8 ≝ ltgt_cn ? eq_ex lt_ex.
47
48 (* operatore ≤ *)
49 ndefinition le_b8 ≝ lege_cn ? eq_ex lt_ex le_ex.
50
51 (* operatore > *)
52 ndefinition gt_b8 ≝ ltgt_cn ? eq_ex gt_ex.
53
54 (* operatore ≥ *)
55 ndefinition ge_b8 ≝ lege_cn ? eq_ex gt_ex ge_ex.
56
57 (* operatore and *)
58 ndefinition and_b8 ≝ fop2_cn ? and_ex.
59
60 (* operatore or *)
61 ndefinition or_b8 ≝ fop2_cn ? or_ex.
62
63 (* operatore xor *)
64 ndefinition xor_b8 ≝ fop2_cn ? xor_ex.
65
66 (* operatore Most Significant Bit *)
67 ndefinition getMSB_b8 ≝ getOPH_cn ? getMSB_ex.
68 ndefinition setMSB_b8 ≝ setOPH_cn ? setMSB_ex.
69
70 (* operatore Least Significant Bit *)
71 ndefinition getLSB_b8 ≝ getOPL_cn ? getLSB_ex.
72 ndefinition setLSB_b8 ≝ setOPL_cn ? setLSB_ex.
73
74 (* operatore rotazione destra con carry *)
75 ndefinition rcr_b8 ≝ opcr_cn ? rcr_ex.
76
77 (* operatore shift destro *)
78 ndefinition shr_b8 ≝ opcr_cn ? rcr_ex false.
79
80 (* operatore rotazione destra *)
81 ndefinition ror_b8 ≝
82 λb.match shr_b8 b with
83  [ pair c b' ⇒ match c with
84   [ true ⇒ setMSB_b8 b' | false ⇒ b' ]].
85
86 (* operatore rotazione sinistra con carry *)
87 ndefinition rcl_b8 ≝ opcl_cn ? rcl_ex.
88
89 (* operatore shift sinistro *)
90 ndefinition shl_b8 ≝ opcl_cn ? rcl_ex false.
91
92 (* operatore rotazione sinistra *)
93 ndefinition rol_b8 ≝
94 λb.match shl_b8 b with
95  [ pair c b' ⇒ match c with
96   [ true ⇒ setLSB_b8 b' | false ⇒ b' ]].
97
98 (* operatore not/complemento a 1 *)
99 ndefinition not_b8 ≝ fop_cn ? not_ex.
100
101 (* operatore somma con data+carry → data+carry *)
102 ndefinition plus_b8_dc_dc ≝ opcl2_cn ? plus_ex_dc_dc.
103
104 (* operatore somma con data+carry → data *)
105 ndefinition plus_b8_dc_d ≝ λc,b1,b2.snd … (plus_b8_dc_dc c b1 b2).
106
107 (* operatore somma con data+carry → c *)
108 ndefinition plus_b8_dc_c ≝ λc,b1,b2.fst … (plus_b8_dc_dc c b1 b2).
109
110 (* operatore somma con data → data+carry *)
111 ndefinition plus_b8_d_dc ≝ opcl2_cn ? plus_ex_dc_dc false.
112
113 (* operatore somma con data → data *)
114 ndefinition plus_b8_d_d ≝ λb1,b2.snd … (plus_b8_d_dc b1 b2).
115
116 (* operatore somma con data → c *)
117 ndefinition plus_b8_d_c ≝ λb1,b2.fst … (plus_b8_d_dc b1 b2).
118
119 (* operatore predecessore *)
120 ndefinition pred_b8 ≝ predsucc_cn ? (eq_ex x0) pred_ex.
121
122 (* operatore successore *)
123 ndefinition succ_b8 ≝ predsucc_cn ? (eq_ex xF) succ_ex.
124
125 (* operatore neg/complemento a 2 *)
126 ndefinition compl_b8 ≝
127 λb:byte8.match getMSB_b8 b with
128  [ true ⇒ succ_b8 (not_b8 b)
129  | false ⇒ not_b8 (pred_b8 b) ].
130
131 (* operatore x in [inf,sup] o in sup],[inf *)
132 ndefinition inrange_b8 ≝
133 λx,inf,sup:byte8.
134  match le_b8 inf sup with
135   [ true ⇒ and_bool | false ⇒ or_bool ]
136  (le_b8 inf x) (le_b8 x sup).
137
138 (* operatore moltiplicazione senza segno: e*e=[0x00,0xE1] *)
139 ndefinition mul_ex ≝
140 λe1,e2:exadecim.match e1 with
141  [ x0 ⇒ match e2 with
142   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x0〉   | x2 ⇒ 〈x0,x0〉   | x3 ⇒ 〈x0,x0〉
143   | x4 ⇒ 〈x0,x0〉   | x5 ⇒ 〈x0,x0〉   | x6 ⇒ 〈x0,x0〉   | x7 ⇒ 〈x0,x0〉
144   | x8 ⇒ 〈x0,x0〉   | x9 ⇒ 〈x0,x0〉   | xA ⇒ 〈x0,x0〉   | xB ⇒ 〈x0,x0〉
145   | xC ⇒ 〈x0,x0〉   | xD ⇒ 〈x0,x0〉   | xE ⇒ 〈x0,x0〉   | xF ⇒ 〈x0,x0〉 ]
146  | x1 ⇒ match e2 with
147   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x1〉   | x2 ⇒ 〈x0,x2〉   | x3 ⇒ 〈x0,x3〉
148   | x4 ⇒ 〈x0,x4〉   | x5 ⇒ 〈x0,x5〉   | x6 ⇒ 〈x0,x6〉   | x7 ⇒ 〈x0,x7〉
149   | x8 ⇒ 〈x0,x8〉   | x9 ⇒ 〈x0,x9〉   | xA ⇒ 〈x0,xA〉   | xB ⇒ 〈x0,xB〉
150   | xC ⇒ 〈x0,xC〉   | xD ⇒ 〈x0,xD〉   | xE ⇒ 〈x0,xE〉   | xF ⇒ 〈x0,xF〉 ]
151  | x2 ⇒ match e2 with
152   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x2〉   | x2 ⇒ 〈x0,x4〉   | x3 ⇒ 〈x0,x6〉
153   | x4 ⇒ 〈x0,x8〉   | x5 ⇒ 〈x0,xA〉   | x6 ⇒ 〈x0,xC〉   | x7 ⇒ 〈x0,xE〉
154   | x8 ⇒ 〈x1,x0〉   | x9 ⇒ 〈x1,x2〉   | xA ⇒ 〈x1,x4〉   | xB ⇒ 〈x1,x6〉
155   | xC ⇒ 〈x1,x8〉   | xD ⇒ 〈x1,xA〉   | xE ⇒ 〈x1,xC〉   | xF ⇒ 〈x1,xE〉 ]
156  | x3 ⇒ match e2 with
157   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x3〉   | x2 ⇒ 〈x0,x6〉   | x3 ⇒ 〈x0,x9〉
158   | x4 ⇒ 〈x0,xC〉   | x5 ⇒ 〈x0,xF〉   | x6 ⇒ 〈x1,x2〉   | x7 ⇒ 〈x1,x5〉
159   | x8 ⇒ 〈x1,x8〉   | x9 ⇒ 〈x1,xB〉   | xA ⇒ 〈x1,xE〉   | xB ⇒ 〈x2,x1〉
160   | xC ⇒ 〈x2,x4〉   | xD ⇒ 〈x2,x7〉   | xE ⇒ 〈x2,xA〉   | xF ⇒ 〈x2,xD〉 ]
161  | x4 ⇒ match e2 with
162   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x4〉   | x2 ⇒ 〈x0,x8〉   | x3 ⇒ 〈x0,xC〉
163   | x4 ⇒ 〈x1,x0〉   | x5 ⇒ 〈x1,x4〉   | x6 ⇒ 〈x1,x8〉   | x7 ⇒ 〈x1,xC〉
164   | x8 ⇒ 〈x2,x0〉   | x9 ⇒ 〈x2,x4〉   | xA ⇒ 〈x2,x8〉   | xB ⇒ 〈x2,xC〉
165   | xC ⇒ 〈x3,x0〉   | xD ⇒ 〈x3,x4〉   | xE ⇒ 〈x3,x8〉   | xF ⇒ 〈x3,xC〉 ]
166  | x5 ⇒ match e2 with
167   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x5〉   | x2 ⇒ 〈x0,xA〉   | x3 ⇒ 〈x0,xF〉
168   | x4 ⇒ 〈x1,x4〉   | x5 ⇒ 〈x1,x9〉   | x6 ⇒ 〈x1,xE〉   | x7 ⇒ 〈x2,x3〉
169   | x8 ⇒ 〈x2,x8〉   | x9 ⇒ 〈x2,xD〉   | xA ⇒ 〈x3,x2〉   | xB ⇒ 〈x3,x7〉
170   | xC ⇒ 〈x3,xC〉   | xD ⇒ 〈x4,x1〉   | xE ⇒ 〈x4,x6〉   | xF ⇒ 〈x4,xB〉 ]
171  | x6 ⇒ match e2 with
172   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x6〉   | x2 ⇒ 〈x0,xC〉   | x3 ⇒ 〈x1,x2〉
173   | x4 ⇒ 〈x1,x8〉   | x5 ⇒ 〈x1,xE〉   | x6 ⇒ 〈x2,x4〉   | x7 ⇒ 〈x2,xA〉
174   | x8 ⇒ 〈x3,x0〉   | x9 ⇒ 〈x3,x6〉   | xA ⇒ 〈x3,xC〉   | xB ⇒ 〈x4,x2〉
175   | xC ⇒ 〈x4,x8〉   | xD ⇒ 〈x4,xE〉   | xE ⇒ 〈x5,x4〉   | xF ⇒ 〈x5,xA〉 ]
176  | x7 ⇒ match e2 with
177   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x7〉   | x2 ⇒ 〈x0,xE〉   | x3 ⇒ 〈x1,x5〉
178   | x4 ⇒ 〈x1,xC〉   | x5 ⇒ 〈x2,x3〉   | x6 ⇒ 〈x2,xA〉   | x7 ⇒ 〈x3,x1〉
179   | x8 ⇒ 〈x3,x8〉   | x9 ⇒ 〈x3,xF〉   | xA ⇒ 〈x4,x6〉   | xB ⇒ 〈x4,xD〉
180   | xC ⇒ 〈x5,x4〉   | xD ⇒ 〈x5,xB〉   | xE ⇒ 〈x6,x2〉   | xF ⇒ 〈x6,x9〉 ]
181  | x8 ⇒ match e2 with
182   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x8〉   | x2 ⇒ 〈x1,x0〉   | x3 ⇒ 〈x1,x8〉
183   | x4 ⇒ 〈x2,x0〉   | x5 ⇒ 〈x2,x8〉   | x6 ⇒ 〈x3,x0〉   | x7 ⇒ 〈x3,x8〉
184   | x8 ⇒ 〈x4,x0〉   | x9 ⇒ 〈x4,x8〉   | xA ⇒ 〈x5,x0〉   | xB ⇒ 〈x5,x8〉
185   | xC ⇒ 〈x6,x0〉   | xD ⇒ 〈x6,x8〉   | xE ⇒ 〈x7,x0〉   | xF ⇒ 〈x7,x8〉 ]
186  | x9 ⇒ match e2 with
187   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,x9〉   | x2 ⇒ 〈x1,x2〉   | x3 ⇒ 〈x1,xB〉
188   | x4 ⇒ 〈x2,x4〉   | x5 ⇒ 〈x2,xD〉   | x6 ⇒ 〈x3,x6〉   | x7 ⇒ 〈x3,xF〉
189   | x8 ⇒ 〈x4,x8〉   | x9 ⇒ 〈x5,x1〉   | xA ⇒ 〈x5,xA〉   | xB ⇒ 〈x6,x3〉
190   | xC ⇒ 〈x6,xC〉   | xD ⇒ 〈x7,x5〉   | xE ⇒ 〈x7,xE〉   | xF ⇒ 〈x8,x7〉 ]
191  | xA ⇒ match e2 with
192   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xA〉   | x2 ⇒ 〈x1,x4〉   | x3 ⇒ 〈x1,xE〉
193   | x4 ⇒ 〈x2,x8〉   | x5 ⇒ 〈x3,x2〉   | x6 ⇒ 〈x3,xC〉   | x7 ⇒ 〈x4,x6〉
194   | x8 ⇒ 〈x5,x0〉   | x9 ⇒ 〈x5,xA〉   | xA ⇒ 〈x6,x4〉   | xB ⇒ 〈x6,xE〉
195   | xC ⇒ 〈x7,x8〉   | xD ⇒ 〈x8,x2〉   | xE ⇒ 〈x8,xC〉   | xF ⇒ 〈x9,x6〉 ]
196  | xB ⇒ match e2 with
197   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xB〉   | x2 ⇒ 〈x1,x6〉   | x3 ⇒ 〈x2,x1〉
198   | x4 ⇒ 〈x2,xC〉   | x5 ⇒ 〈x3,x7〉   | x6 ⇒ 〈x4,x2〉   | x7 ⇒ 〈x4,xD〉
199   | x8 ⇒ 〈x5,x8〉   | x9 ⇒ 〈x6,x3〉   | xA ⇒ 〈x6,xE〉   | xB ⇒ 〈x7,x9〉
200   | xC ⇒ 〈x8,x4〉   | xD ⇒ 〈x8,xF〉   | xE ⇒ 〈x9,xA〉   | xF ⇒ 〈xA,x5〉 ]
201  | xC ⇒ match e2 with
202   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xC〉   | x2 ⇒ 〈x1,x8〉   | x3 ⇒ 〈x2,x4〉
203   | x4 ⇒ 〈x3,x0〉   | x5 ⇒ 〈x3,xC〉   | x6 ⇒ 〈x4,x8〉   | x7 ⇒ 〈x5,x4〉
204   | x8 ⇒ 〈x6,x0〉   | x9 ⇒ 〈x6,xC〉   | xA ⇒ 〈x7,x8〉   | xB ⇒ 〈x8,x4〉
205   | xC ⇒ 〈x9,x0〉   | xD ⇒ 〈x9,xC〉   | xE ⇒ 〈xA,x8〉   | xF ⇒ 〈xB,x4〉 ]
206  | xD ⇒ match e2 with
207   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xD〉   | x2 ⇒ 〈x1,xA〉   | x3 ⇒ 〈x2,x7〉
208   | x4 ⇒ 〈x3,x4〉   | x5 ⇒ 〈x4,x1〉   | x6 ⇒ 〈x4,xE〉   | x7 ⇒ 〈x5,xB〉
209   | x8 ⇒ 〈x6,x8〉   | x9 ⇒ 〈x7,x5〉   | xA ⇒ 〈x8,x2〉   | xB ⇒ 〈x8,xF〉
210   | xC ⇒ 〈x9,xC〉   | xD ⇒ 〈xA,x9〉   | xE ⇒ 〈xB,x6〉   | xF ⇒ 〈xC,x3〉 ]
211  | xE ⇒ match e2 with
212   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xE〉   | x2 ⇒ 〈x1,xC〉   | x3 ⇒ 〈x2,xA〉
213   | x4 ⇒ 〈x3,x8〉   | x5 ⇒ 〈x4,x6〉   | x6 ⇒ 〈x5,x4〉   | x7 ⇒ 〈x6,x2〉
214   | x8 ⇒ 〈x7,x0〉   | x9 ⇒ 〈x7,xE〉   | xA ⇒ 〈x8,xC〉   | xB ⇒ 〈x9,xA〉
215   | xC ⇒ 〈xA,x8〉   | xD ⇒ 〈xB,x6〉   | xE ⇒ 〈xC,x4〉   | xF ⇒ 〈xD,x2〉 ]
216  | xF ⇒ match e2 with
217   [ x0 ⇒ 〈x0,x0〉   | x1 ⇒ 〈x0,xF〉   | x2 ⇒ 〈x1,xE〉   | x3 ⇒ 〈x2,xD〉
218   | x4 ⇒ 〈x3,xC〉   | x5 ⇒ 〈x4,xB〉   | x6 ⇒ 〈x5,xA〉   | x7 ⇒ 〈x6,x9〉
219   | x8 ⇒ 〈x7,x8〉   | x9 ⇒ 〈x8,x7〉   | xA ⇒ 〈x9,x6〉   | xB ⇒ 〈xA,x5〉
220   | xC ⇒ 〈xB,x4〉   | xD ⇒ 〈xC,x3〉   | xE ⇒ 〈xD,x2〉   | xF ⇒ 〈xE,x1〉 ]
221  ].
222
223 (* correzione per somma su BCD *)
224 (* input: halfcarry,carry,X(BCD+BCD) *)
225 (* output: X',carry' *)
226 ndefinition daa_b8 ≝
227 λh,c:bool.λX:byte8.
228  match lt_b8 X 〈x9,xA〉 with
229   (* [X:0x00-0x99] *)
230   (* c' = c *)
231   (* X' = [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + [c=1 ? 0x60 : 0x00]
232           [(b16l X):0xA-0xF] X + 0x06 + [c=1 ? 0x60 : 0x00] *)
233   [ true ⇒
234    let X' ≝ match (lt_ex (cnL ? X) xA) ⊗ (⊖h) with
235     [ true ⇒ X
236     | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
237    let X'' ≝ match c with
238     [ true ⇒ plus_b8_d_d X' 〈x6,x0〉
239     | false ⇒ X' ] in
240    pair … X'' c
241   (* [X:0x9A-0xFF] *)
242   (* c' = 1 *)
243   (* X' = [X:0x9A-0xFF]
244           [(b16l X):0x0-0x9] X + [h=1 ? 0x06 : 0x00] + 0x60
245           [(b16l X):0xA-0xF] X + 0x6 + 0x60 *) 
246   | false ⇒
247    let X' ≝ match (lt_ex (cnL ? X) xA) ⊗ (⊖h) with
248     [ true ⇒ X
249     | false ⇒ plus_b8_d_d X 〈x0,x6〉 ] in
250    let X'' ≝ plus_b8_d_d X' 〈x6,x0〉 in
251    pair … X'' true
252   ].
253
254 (* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
255 nlet rec div_b8_ex_aux (divd:byte8) (divs:byte8) (molt:exadecim) (q:exadecim) (n:nat) on n ≝
256  let w' ≝ plus_b8_d_d divd (compl_b8 divs) in
257   match n with
258   [ O ⇒ match le_b8 divs divd with
259    [ true ⇒ triple … (or_ex molt q) (cnL ? w') (⊖ (eq_ex (cnH ? w') x0))
260    | false ⇒ triple … q (cnL ? divd) (⊖ (eq_ex (cnH ? divd) x0)) ]
261   | S n' ⇒ match le_b8 divs divd with
262    [ true ⇒ div_b8_ex_aux w' (ror_b8 divs) (ror_ex molt) (or_ex molt q) n'
263    | false ⇒ div_b8_ex_aux divd (ror_b8 divs) (ror_ex molt) q n' ]].
264
265 ndefinition div_b8_ex ≝
266 λb:byte8.λe:exadecim.match eq_ex e x0 with
267 (* la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato *)
268  [ true ⇒ triple … xF (cnL ? b) true
269  | false ⇒ match eq_b8 b 〈x0,x0〉 with
270 (* 0 diviso qualsiasi cosa diverso da 0 da' q=0 r=0 o=false *)
271   [ true ⇒ triple … x0 x0 false
272 (* 1) e' una divisione sensata che produrra' overflow/risultato *)
273 (* 2) parametri: dividendo, divisore, moltiplicatore, quoziente, contatore *)
274 (* 3) ad ogni ciclo il divisore e il moltiplicatore vengono scalati di 1 a dx *)
275 (* 4) il moltiplicatore e' la quantita' aggiunta al quoziente se il divisore *)
276 (*    puo' essere sottratto al dividendo *) 
277   | false ⇒ div_b8_ex_aux b (nat_it ? rol_b8 〈x0,e〉 nat3) x8 x0 nat3 ]].
278
279 (* byte ricorsivi *)
280 ninductive rec_byte8 : byte8 → Type ≝
281   b8_O : rec_byte8 〈x0,x0〉
282 | b8_S : ∀n.rec_byte8 n → rec_byte8 (succ_b8 n).
283
284 (* byte → byte ricorsivi *)
285 ndefinition b8_to_recb8_aux1 : Πn.rec_byte8 〈n,x0〉 → rec_byte8 〈succ_ex n,x0〉 ≝
286 λn.λrecb:rec_byte8 〈n,x0〉.
287  b8_S 〈n,xF〉 (b8_S 〈n,xE〉 (b8_S 〈n,xD〉 (b8_S 〈n,xC〉 (
288  b8_S 〈n,xB〉 (b8_S 〈n,xA〉 (b8_S 〈n,x9〉 (b8_S 〈n,x8〉 (
289  b8_S 〈n,x7〉 (b8_S 〈n,x6〉 (b8_S 〈n,x5〉 (b8_S 〈n,x4〉 (
290  b8_S 〈n,x3〉 (b8_S 〈n,x2〉 (b8_S 〈n,x1〉 (b8_S 〈n,x0〉 recb))))))))))))))).
291
292 (* ... cifra esadecimale superiore *)
293 nlet rec b8_to_recb8_aux2 (n:exadecim) (r:rec_exadecim n) on r ≝
294  match r return λx.λy:rec_exadecim x.rec_byte8 〈x,x0〉 with
295   [ ex_O ⇒ b8_O
296   | ex_S t n' ⇒ b8_to_recb8_aux1 ? (b8_to_recb8_aux2 t n')
297   ].
298
299 (* ... cifra esadecimale inferiore *)
300 ndefinition b8_to_recb8_aux3 : Πn1,n2.rec_byte8 〈n1,x0〉 → rec_byte8 〈n1,n2〉 ≝
301 λn1,n2.λrecb:rec_byte8 〈n1,x0〉.
302  match n2 return λx.rec_byte8 〈n1,x〉 with
303   [ x0 ⇒ recb
304   | x1 ⇒ b8_S 〈n1,x0〉 recb
305   | x2 ⇒ b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)
306   | x3 ⇒ b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))
307   | x4 ⇒ b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))
308   | x5 ⇒ b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
309          b8_S 〈n1,x0〉 recb))))
310   | x6 ⇒ b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
311          b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))
312   | x7 ⇒ b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
313          b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))
314   | x8 ⇒ b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (
315          b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))
316   | x9 ⇒ b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (
317          b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
318          b8_S 〈n1,x0〉 recb))))))))
319   | xA ⇒ b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (
320          b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
321          b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))
322   | xB ⇒ b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (
323          b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
324          b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))))))
325   | xC ⇒ b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (
326          b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (
327          b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))))
328   | xD ⇒ b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (
329          b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (
330          b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
331          b8_S 〈n1,x0〉 recb))))))))))))
332   | xE ⇒ b8_S 〈n1,xD〉 (b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (
333          b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (
334          b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
335          b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))))))
336   | xF ⇒ b8_S 〈n1,xE〉 (b8_S 〈n1,xD〉 (b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (
337          b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (
338          b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
339          b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))))))))))
340   ].
341
342 (*
343 nlemma b8_to_recb8 : Πb.rec_byte8 b.
344  #b; nletin K ≝ (b8_to_recb8_aux3 
345      (b8h b) (b8l b) (b8_to_recb8_aux2 (b8h b) (ex_to_recex (b8h b))));
346  ncases b in K; #e1; #e2; #K; napply K;
347 nqed.
348 *)
349
350 ndefinition b8_to_recb8 : Πb.rec_byte8 b ≝
351 λb.match b with
352  [ mk_comp_num h l ⇒ b8_to_recb8_aux3 h l (b8_to_recb8_aux2 h (ex_to_recex h)) ].
353
354 (* ottali → esadecimali *)
355 ndefinition b8_of_bit ≝
356 λn.match n with
357  [ t00 ⇒ 〈x0,x0〉 | t01 ⇒ 〈x0,x1〉 | t02 ⇒ 〈x0,x2〉 | t03 ⇒ 〈x0,x3〉
358  | t04 ⇒ 〈x0,x4〉 | t05 ⇒ 〈x0,x5〉 | t06 ⇒ 〈x0,x6〉 | t07 ⇒ 〈x0,x7〉
359  | t08 ⇒ 〈x0,x8〉 | t09 ⇒ 〈x0,x9〉 | t0A ⇒ 〈x0,xA〉 | t0B ⇒ 〈x0,xB〉
360  | t0C ⇒ 〈x0,xC〉 | t0D ⇒ 〈x0,xD〉 | t0E ⇒ 〈x0,xE〉 | t0F ⇒ 〈x0,xF〉
361  | t10 ⇒ 〈x1,x0〉 | t11 ⇒ 〈x1,x1〉 | t12 ⇒ 〈x1,x2〉 | t13 ⇒ 〈x1,x3〉
362  | t14 ⇒ 〈x1,x4〉 | t15 ⇒ 〈x1,x5〉 | t16 ⇒ 〈x1,x6〉 | t17 ⇒ 〈x1,x7〉
363  | t18 ⇒ 〈x1,x8〉 | t19 ⇒ 〈x1,x9〉 | t1A ⇒ 〈x1,xA〉 | t1B ⇒ 〈x1,xB〉
364  | t1C ⇒ 〈x1,xC〉 | t1D ⇒ 〈x1,xD〉 | t1E ⇒ 〈x1,xE〉 | t1F ⇒ 〈x1,xF〉
365  ].