Debian Codegraph @

src/Data/Bytestring/Primitive.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Primitive bytestrings: simple bindings to Haskell types and functions
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible #-}
8
9 module Data.Bytestring.Primitive where
10
11 open import Agda.Builtin.Nat using (Nat)
12 open import Agda.Builtin.String using (String)
13 open import Agda.Builtin.Word using (Word64)
14 open import Data.Word8.Primitive using (Word8)
15
16 -- NB: the bytestring package uses `Int` as the indexing type which
17 -- Haskell's base specifies as:
18 --
19 -- > A fixed-precision integer type with at least the range
20 -- > [-2^29 .. 2^29-1]. The exact range for a given implementation
21 -- > can be determined by using minBound and maxBound from the
22 -- > Bounded class.
23 --
24 -- There is no ergonomic way to encode that in a type-safe manner.
25 -- For now we use `Word64` with the understanding that using indices
26 -- greater than 2^29-1 may lead to undefined behaviours...
27
28 postulate
29 Bytestring : Set
30 index : Bytestring → Word64 → Word8
31 length : Bytestring → Nat
32 take : Word64 → Bytestring → Bytestring
33 drop : Word64 → Bytestring → Bytestring
34 show : Bytestring → String
35
36 {-# FOREIGN GHC import qualified Data.ByteString as B #-}
37 {-# FOREIGN GHC import qualified Data.Text as T #-}
38
39 {-# COMPILE GHC Bytestring = type B.ByteString #-}
40 {-# COMPILE GHC index = \ buf idx -> B.index buf (fromIntegral idx) #-}
41 {-# COMPILE GHC length = \ buf -> fromIntegral (B.length buf) #-}
42 {-# COMPILE GHC take = B.take . fromIntegral #-}
43 {-# COMPILE GHC drop = B.drop . fromIntegral #-}
44 {-# COMPILE GHC show = T.pack . Prelude.show #-}