Debian Codegraph @

src/Data/Word8/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Bytes: base type and functions
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible #-}
8
9 module Data.Word8.Base where
10
11 open import Agda.Builtin.Bool using (Bool; true; false)
12 open import Agda.Builtin.Char using (Char)
13 open import Agda.Builtin.Nat using (Nat; _==_)
14 open import Agda.Builtin.Unit using (⊤)
15 open import Data.Fin.Base as Fin using (Fin)
16 open import Data.Vec.Base as Vec using (Vec; []; _∷_)
17 open import Function.Base using (_$_; _|>_)
18
19 ------------------------------------------------------------------------------
20 -- Re-export type and operations
21
22 open import Data.Word8.Primitive as Prim public
23 using ( Word8
24 ; _+_
25 ; show
26 )
27 renaming ( fromNat to fromℕ
28 ; toNat to toℕ
29 )
30
31 testBit : Word8 → Fin 8 → Bool
32 testBit w i = Prim.testBit w (Fin.toℕ i)
33
34 _[_]≔_ : Word8 → Fin 8 → Bool → Word8
35 w [ i ]≔ false = Prim.clearBit w (Fin.toℕ i)
36 w [ i ]≔ true = Prim.setBit w (Fin.toℕ i)
37
38 ------------------------------------------------------------------------------
39 -- Basic functions
40
41 toBits : Word8 → Vec Bool 8
42 toBits w = Vec.tabulate (testBit w)
43
44 fromBits : Vec Bool 8 → Word8
45 fromBits bs = Vec.foldl′ _|>_ (fromℕ 0)
46 $ Vec.zipWith (λ i b → _[ i ]≔ b) (Vec.allFin 8) bs
47
48 _≡ᵇ_ : Word8 → Word8 → Bool
49 w ≡ᵇ w′ = toℕ w == toℕ w′