(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* BitVector.ma: Fixed length bitvectors, and common operations on them. *) (* Most functions are specialised versions of those found in *) (* Vector.ma as a courtesy, or Boolean functions lifted into *) (* BitVector variants. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) include "Universes.ma". include "Vector.ma". include "List.ma". include "Nat.ma". include "Bool.ma". (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* Common synonyms. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) ndefinition BitVector ≝ λn: Nat. Vector Bool n. ndefinition Bit ≝ BitVector (S Z). ndefinition Nibble ≝ BitVector (S (S (S (S Z)))). ndefinition Byte7 ≝ BitVector (S (S (S (S (S (S (S Z))))))). ndefinition Byte ≝ BitVector (S (S (S (S (S (S (S (S Z)))))))). ndefinition Word ≝ BitVector (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S (S Z)))))))))))))))). ndefinition Word11 ≝ BitVector (S (S (S (S (S (S (S (S (S (S (S (S (S Z))))))))))))). (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* Lookup. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) ndefinition get_index ≝ λn: Nat. λb: BitVector n. λm: Nat. get_index Bool n b m. interpretation "BitVector get_index" 'get_index b n = (get_index ? b n). ndefinition set_index ≝ λn: Nat. λb: BitVector n. λm: Nat. set_index Bool n b m. ndefinition drop ≝ λn: Nat. λb: BitVector n. λm: Nat. drop Bool n b m. (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* Creating bitvectors from scratch. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) ndefinition zero ≝ λn: Nat. replicate Bool n False. ndefinition max ≝ λn: Nat. replicate Bool n True. ndefinition append ≝ λm, n: Nat. λb: BitVector m. λc: BitVector n. append Bool m n b c. interpretation "BitVector append" 'append b c = (append b c). ndefinition pad ≝ λm, n: Nat. λb: BitVector n. let padding ≝ replicate Bool m False in append Bool m n padding b. (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* Other manipulations. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) ndefinition reverse ≝ λn: Nat. λb: BitVector n. reverse Bool n b. ndefinition length ≝ λn: Nat. λb: BitVector n. length Bool n b. (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* Logical operations. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) ndefinition conjunction ≝ λn: Nat. λb: BitVector n. λc: BitVector n. zip_with Bool Bool Bool n conjunction b c. interpretation "BitVector conjunction" 'conjunction b c = (conjunction ? b c). ndefinition inclusive_disjunction ≝ λn: Nat. λb: BitVector n. λc: BitVector n. zip_with Bool Bool Bool n inclusive_disjunction b c. interpretation "BitVector inclusive disjunction" 'inclusive_disjunction b c = (inclusive_disjunction ? b c). ndefinition exclusive_disjunction ≝ λn: Nat. λb: BitVector n. λc: BitVector n. zip_with Bool Bool Bool n exclusive_disjunction b c. interpretation "BitVector exclusive disjunction" 'exclusive_disjunction b c = (exclusive_disjunction ? b c). ndefinition negation ≝ λn: Nat. λb: BitVector n. map Bool Bool n (negation) b. interpretation "BitVector negation" 'negation b c = (negation ? b c). (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* Rotates and shifts. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) ndefinition rotate_left ≝ λm, n: Nat. λb: BitVector n. rotate_left Bool n m b. ndefinition rotate_right ≝ λm, n: Nat. λb: BitVector n. rotate_right Bool n m b. ndefinition shift_left ≝ λm, n: Nat. λb: BitVector n. λc: Bool. shift_left Bool n m b c. ndefinition shift_right ≝ λm, n: Nat. λb: BitVector n. λc: Bool. shift_right Bool n m b c. (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) (* Conversions to and from lists and natural numbers. *) (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= *) ndefinition list_of_bitvector ≝ λn: Nat. λb: BitVector n. list_of_vector Bool n b. ndefinition bitvector_of_list ≝ λl: List Bool. vector_of_list Bool l. nlet rec bitvector_of_nat_aux (n: Nat) on n ≝ let divrem ≝ divide_with_remainder n (S (S Z)) in let div ≝ first Nat Nat divrem in let rem ≝ second Nat Nat divrem in match div with [ Z ⇒ match rem with [ Z ⇒ Empty Bool | S r ⇒ ? (True :: (bitvector_of_nat_aux Z)) ] | S d ⇒ match rem with [ Z ⇒ ? (False :: (bitvector_of_nat_aux (S d))) | S r ⇒ ? (True :: (bitvector_of_nat_aux (S d))) ] ]. //. nqed. ndefinition bitvector_of_nat ≝ λm, n: Nat. let biglist ≝ reverse Bool (bitvector_of_nat_aux m) in let size ≝ length Bool biglist in let bitvector ≝ bitvector_of_list biglist in let difference ≝ n - size in pad difference size bitvector. nlet rec nat_of_bitvector (n: Nat) (b: BitVector n) on b ≝ match b with [ Empty ⇒ Z | Cons o hd tl ⇒ let hdval ≝ match hd with [ True ⇒ S Z | False ⇒ Z ] in ((exponential (S (S Z)) o) * hdval) + nat_of_bitvector o tl ].