(* ********************************************************************** *)
(* Progetto FreeScale *)
(* *)
-(* Sviluppato da: Cosimo Oliboni, oliboni@cs.unibo.it *)
-(* Cosimo Oliboni, oliboni@cs.unibo.it *)
+(* Sviluppato da: Ing. Cosimo Oliboni, oliboni@cs.unibo.it *)
+(* Sviluppo: 2008-2010 *)
(* *)
(* ********************************************************************** *)
(* operatore < *)
ndefinition lt_b8 ≝
-λb1,b2:byte8.match lt_ex (b8h b1) (b8h b2) with
- [ true ⇒ true
- | false ⇒ match gt_ex (b8h b1) (b8h b2) with
- [ true ⇒ false
- | false ⇒ lt_ex (b8l b1) (b8l b2) ]].
+λb1,b2:byte8.
+ (lt_ex (b8h b1) (b8h b2)) ⊕
+ ((eq_ex (b8h b1) (b8h b2)) ⊗ (lt_ex (b8l b1) (b8l b2))).
(* operatore ≤ *)
-ndefinition le_b8 ≝ λb1,b2:byte8.(eq_b8 b1 b2) ⊕ (lt_b8 b1 b2).
+ndefinition le_b8 ≝
+λb1,b2:byte8.
+ (lt_ex (b8h b1) (b8h b2)) ⊕
+ ((eq_ex (b8h b1) (b8h b2)) ⊗ (le_ex (b8l b1) (b8l b2))).
(* operatore > *)
-ndefinition gt_b8 ≝ λb1,b2:byte8.⊖ (le_b8 b1 b2).
+ndefinition gt_b8 ≝
+λb1,b2:byte8.
+ (gt_ex (b8h b1) (b8h b2)) ⊕
+ ((eq_ex (b8h b1) (b8h b2)) ⊗ (gt_ex (b8l b1) (b8l b2))).
(* operatore ≥ *)
-ndefinition ge_b8 ≝ λb1,b2:byte8.⊖ (lt_b8 b1 b2).
+ndefinition ge_b8 ≝
+λb1,b2:byte8.
+ (gt_ex (b8h b1) (b8h b2)) ⊕
+ ((eq_ex (b8h b1) (b8h b2)) ⊗ (ge_ex (b8l b1) (b8l b2))).
(* operatore and *)
ndefinition and_b8 ≝
(* operatore shift destro *)
ndefinition shr_b8 ≝
-λb:byte8.match rcr_ex (b8h b) false with
+λb:byte8.match shr_ex (b8h b) with
[ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
[ pair bl' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
(* operatore rotazione destra *)
ndefinition ror_b8 ≝
-λb:byte8.match rcr_ex (b8h b) false with
+λb:byte8.match shr_ex (b8h b) with
[ pair bh' c' ⇒ match rcr_ex (b8l b) c' with
[ pair bl' c'' ⇒ match c'' with
[ true ⇒ mk_byte8 (or_ex x8 bh') bl'
| false ⇒ mk_byte8 bh' bl' ]]].
(* operatore rotazione destra n-volte *)
-nlet rec ror_b8_n (b:byte8) (n:nat) on n ≝
- match n with
- [ O ⇒ b
- | S n' ⇒ ror_b8_n (ror_b8 b) n' ].
+nlet rec ror_b8_n (b:byte8) (o:oct) (ro:rec_oct o) on ro ≝
+ match ro with
+ [ oc_O ⇒ b
+ | oc_S o' n' ⇒ ror_b8_n (ror_b8 b) o' n' ].
(* operatore rotazione sinistra con carry *)
ndefinition rcl_b8 ≝
(* operatore shift sinistro *)
ndefinition shl_b8 ≝
-λb:byte8.match rcl_ex (b8l b) false with
+λb:byte8.match shl_ex (b8l b) with
[ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
[ pair bh' c'' ⇒ pair … (mk_byte8 bh' bl') c'' ]].
(* operatore rotazione sinistra *)
ndefinition rol_b8 ≝
-λb:byte8.match rcl_ex (b8l b) false with
+λb:byte8.match shl_ex (b8l b) with
[ pair bl' c' ⇒ match rcl_ex (b8h b) c' with
[ pair bh' c'' ⇒ match c'' with
[ true ⇒ mk_byte8 bh' (or_ex x1 bl')
| false ⇒ mk_byte8 bh' bl' ]]].
(* operatore rotazione sinistra n-volte *)
-nlet rec rol_b8_n (b:byte8) (n:nat) on n ≝
- match n with
- [ O ⇒ b
- | S n' ⇒ rol_b8_n (rol_b8 b) n' ].
+nlet rec rol_b8_n (b:byte8) (o:oct) (ro:rec_oct o) on ro ≝
+ match ro with
+ [ oc_O ⇒ b
+ | oc_S o' n' ⇒ rol_b8_n (rol_b8 b) o' n' ].
(* operatore not/complemento a 1 *)
ndefinition not_b8 ≝
ndefinition plus_b8_dc_dc ≝
λb1,b2:byte8.λc:bool.
match plus_ex_dc_dc (b8l b1) (b8l b2) c with
- [ pair l c ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c with
- [ pair h c' ⇒ pair … 〈h,l〉 c' ]].
+ [ pair l c' ⇒ match plus_ex_dc_dc (b8h b1) (b8h b2) c' with
+ [ pair h c'' ⇒ pair … 〈h,l〉 c'' ]].
(* operatore somma con data+carry → data *)
ndefinition plus_b8_dc_d ≝
λb1,b2:byte8.λc:bool.
match plus_ex_dc_dc (b8l b1) (b8l b2) c with
- [ pair l c ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c,l〉 ].
+ [ pair l c' ⇒ 〈plus_ex_dc_d (b8h b1) (b8h b2) c',l〉 ].
(* operatore somma con data+carry → c *)
ndefinition plus_b8_dc_c ≝
pair … X'' true
].
+(* divisione senza segno (secondo la logica delle ALU): (quoziente resto) overflow *)
+nlet rec div_b8_ex_aux (divd:byte8) (divs:byte8) (molt:exadecim) (q:exadecim) (qu:quatern) (rqu:rec_quatern qu) on rqu ≝
+ let w' ≝ plus_b8_d_d divd (compl_b8 divs) in
+ match rqu with
+ [ qu_O ⇒ match le_b8 divs divd with
+ [ true ⇒ triple … (or_ex molt q) (b8l w') (⊖ (eq_ex (b8h w') x0))
+ | false ⇒ triple … q (b8l divd) (⊖ (eq_ex (b8h divd) x0)) ]
+ | qu_S qu' rqu' ⇒ match le_b8 divs divd with
+ [ true ⇒ div_b8_ex_aux w' (ror_b8 divs) (ror_ex molt) (or_ex molt q) qu' rqu'
+ | false ⇒ div_b8_ex_aux divd (ror_b8 divs) (ror_ex molt) q qu' rqu' ]].
+
+ndefinition div_b8_ex ≝
+λb:byte8.λe:exadecim.match eq_ex e x0 with
+(*
+ la combinazione n/0 e' illegale, segnala solo overflow senza dare risultato
+*)
+ [ true ⇒ triple … xF (b8l b) true
+ | false ⇒ match eq_b8 b 〈x0,x0〉 with
+(* 0 diviso qualsiasi cosa diverso da 0 da' q=0 r=0 o=false *)
+ [ true ⇒ triple … x0 x0 false
+(* 1) e' una divisione sensata che produrra' overflow/risultato *)
+(* 2) parametri: dividendo, divisore, moltiplicatore, quoziente, contatore *)
+(* 3) ad ogni ciclo il divisore e il moltiplicatore vengono scalati di 1 a dx *)
+(* 4) il moltiplicatore e' la quantita' aggiunta al quoziente se il divisore *)
+(* puo' essere sottratto al dividendo *)
+ | false ⇒ div_b8_ex_aux b (rol_b8_n 〈x0,e〉 ? (oct_to_recoct o3)) x8 x0 ? (qu_to_recqu q3) ]].
+
+(* operatore x in [inf,sup] o in sup],[inf *)
+ndefinition inrange_b8 ≝
+λx,inf,sup:byte8.
+ match le_b8 inf sup with
+ [ true ⇒ and_bool | false ⇒ or_bool ]
+ (le_b8 inf x) (le_b8 x sup).
+
(* iteratore sui byte *)
ndefinition forall_b8 ≝
λP.
(* byte → byte ricorsivi *)
ndefinition b8_to_recb8_aux1 : Πn.rec_byte8 〈n,x0〉 → rec_byte8 〈succ_ex n,x0〉 ≝
λn.λrecb:rec_byte8 〈n,x0〉.
- b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (
- b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb))))))))))))))).
+ b8_S 〈n,xF〉 (b8_S 〈n,xE〉 (b8_S 〈n,xD〉 (b8_S 〈n,xC〉 (
+ b8_S 〈n,xB〉 (b8_S 〈n,xA〉 (b8_S 〈n,x9〉 (b8_S 〈n,x8〉 (
+ b8_S 〈n,x7〉 (b8_S 〈n,x6〉 (b8_S 〈n,x5〉 (b8_S 〈n,x4〉 (
+ b8_S 〈n,x3〉 (b8_S 〈n,x2〉 (b8_S 〈n,x1〉 (b8_S 〈n,x0〉 recb))))))))))))))).
(* ... cifra esadecimale superiore *)
nlet rec b8_to_recb8_aux2 (n:exadecim) (r:rec_exadecim n) on r ≝
λn1,n2.λrecb:rec_byte8 〈n1,x0〉.
match n2 return λx.rec_byte8 〈n1,x〉 with
[ x0 ⇒ recb
- | x1 ⇒ b8_S ? recb
- | x2 ⇒ b8_S ? (b8_S ? recb)
- | x3 ⇒ b8_S ? (b8_S ? (b8_S ? recb))
- | x4 ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? recb)))
- | x5 ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb))))
- | x6 ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb)))))
- | x7 ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb))))))
- | x8 ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb)))))))
- | x9 ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb))))))))
- | xA ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb)))))))))
- | xB ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb))))))))))
- | xC ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb)))))))))))
- | xD ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb))))))))))))
- | xE ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb)))))))))))))
- | xF ⇒ b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? (b8_S ? recb))))))))))))))
+ | x1 ⇒ b8_S 〈n1,x0〉 recb
+ | x2 ⇒ b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)
+ | x3 ⇒ b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))
+ | x4 ⇒ b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))
+ | x5 ⇒ b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
+ b8_S 〈n1,x0〉 recb))))
+ | x6 ⇒ b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
+ b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))
+ | x7 ⇒ b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
+ b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))
+ | x8 ⇒ b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (
+ b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))
+ | x9 ⇒ b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (
+ b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
+ b8_S 〈n1,x0〉 recb))))))))
+ | xA ⇒ b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (
+ b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
+ b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))
+ | xB ⇒ b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (
+ b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
+ b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))))))
+ | xC ⇒ b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (
+ b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (
+ b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))))
+ | xD ⇒ b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (
+ b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (
+ b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (
+ b8_S 〈n1,x0〉 recb))))))))))))
+ | xE ⇒ b8_S 〈n1,xD〉 (b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (b8_S 〈n1,xA〉 (
+ b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (b8_S 〈n1,x6〉 (
+ b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (b8_S 〈n1,x2〉 (
+ b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb)))))))))))))
+ | xF ⇒ b8_S 〈n1,xE〉 (b8_S 〈n1,xD〉 (b8_S 〈n1,xC〉 (b8_S 〈n1,xB〉 (
+ b8_S 〈n1,xA〉 (b8_S 〈n1,x9〉 (b8_S 〈n1,x8〉 (b8_S 〈n1,x7〉 (
+ b8_S 〈n1,x6〉 (b8_S 〈n1,x5〉 (b8_S 〈n1,x4〉 (b8_S 〈n1,x3〉 (
+ b8_S 〈n1,x2〉 (b8_S 〈n1,x1〉 (b8_S 〈n1,x0〉 recb))))))))))))))
].
-ndefinition b8_to_recb8 ≝
-λn.b8_to_recb8_aux3 (b8h n) (b8l n) (b8_to_recb8_aux2 (b8h n) (ex_to_recex (b8h n))).
+(*
+nlemma b8_to_recb8 : Πb.rec_byte8 b.
+ #b; nletin K ≝ (b8_to_recb8_aux3
+ (b8h b) (b8l b) (b8_to_recb8_aux2 (b8h b) (ex_to_recex (b8h b))));
+ ncases b in K; #e1; #e2; #K; napply K;
+nqed.
+*)
+
+ndefinition b8_to_recb8 : Πb.rec_byte8 b ≝
+λb.match b with [ mk_byte8 h l ⇒ b8_to_recb8_aux3 h l (b8_to_recb8_aux2 h (ex_to_recex h)) ].
(* ottali → esadecimali *)
ndefinition b8_of_bit ≝