Skip to content

Commit 307152f

Browse files
authored
[ new ] Word8, Bytestring, Bytestring builder (#2376)
* [ admin ] deprecate Word -> Word64 * [ new ] a type of bytes * [ new ] Bytestrings and builders * [ test ] for bytestrings, builders, word conversion, show, etc. * [ ci ] bump ghc & cabal numbers * [ fix ] actually set the bits ya weapon * [ ci ] bump options to match makefile * [ ci ] more heap * [ more ] add hexadecimal show functions too
1 parent ad0fb0e commit 307152f

39 files changed

+937
-158
lines changed

.github/workflows/ci-ubuntu.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ on:
4747
########################################################################
4848

4949
env:
50-
GHC_VERSION: 8.10.7
51-
CABAL_VERSION: 3.6.2.0
50+
GHC_VERSION: 9.8.2
51+
CABAL_VERSION: 3.10.3.0
5252
CABAL_INSTALL: cabal v1-install --ghc-options='-O1 +RTS -M6G -RTS'
5353
# CABAL_INSTALL: cabal install --overwrite-policy=always --ghc-options='-O1 +RTS -M6G -RTS'
54-
AGDA: agda -Werror +RTS -M3.5G -H3.5G -A128M -RTS -i. -isrc -idoc
54+
AGDA: agda -Werror +RTS -M5G -H3.5G -A128M -RTS -i. -isrc -idoc
5555

5656
jobs:
5757
test-stdlib:

CHANGELOG.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Non-backwards compatible changes
3030
* The definitions in `Algebra.Module.Morphism.Construct.Identity` are now
3131
parametrized by _raw_ bundles, and as such take a proof of reflexivity.
3232
* The module `IO.Primitive` was moved to `IO.Primitive.Core`.
33+
* The modules in the `Data.Word` hierarchy were moved to the `Data.Word64`
34+
one instead.
3335

3436
Other major improvements
3537
------------------------
@@ -107,6 +109,16 @@ Deprecated names
107109
untilRight ↦ untilInj₂
108110
```
109111

112+
* In `Data.Float.Base`:
113+
```agda
114+
toWord ↦ toWord64
115+
```
116+
117+
* In `Data.Float.Properties`:
118+
```agda
119+
toWord-injective ↦ toWord64-injective
120+
```
121+
110122
New modules
111123
-----------
112124

@@ -231,6 +243,31 @@ New modules
231243
Data.Vec.Bounded.Show
232244
```
233245

246+
* Word64 literals and bit-based functions:
247+
```agda
248+
Data.Word64.Literals
249+
Data.Word64.Unsafe
250+
Data.Word64.Show
251+
```
252+
253+
* A type of bytes:
254+
```agda
255+
Data.Word8.Primitive
256+
Data.Word8.Base
257+
Data.Word8.Literals
258+
Data.Word8.Show
259+
```
260+
261+
* Bytestrings and builders:
262+
```agda
263+
Data.Bytestring.Base
264+
Data.Bytestring.Builder.Base
265+
Data.Bytestring.Builder.Primitive
266+
Data.Bytestring.IO
267+
Data.Bytestring.IO.Primitive
268+
Data.Bytestring.Primitive
269+
```
270+
234271
* Decidability for the subset relation on lists:
235272
```agda
236273
Data.List.Relation.Binary.Subset.DecSetoid (_⊆?_)
@@ -402,6 +439,11 @@ Additions to existing modules
402439
*-cancelʳ-nonZero : AlmostRightCancellative 0# *
403440
```
404441

442+
* In `Data.Bool.Show`:
443+
```agda
444+
showBit : Bool → Char
445+
```
446+
405447
* In `Data.Container.Indexed.Core`:
406448
```agda
407449
Subtrees o c = (r : Response c) → X (next c r)
@@ -711,9 +753,10 @@ Additions to existing modules
711753
_>>_ : IO A → IO B → IO B
712754
```
713755

714-
* In `Data.Word.Base`:
756+
* In `Data.Word64.Base`:
715757
```agda
716758
_≤_ : Rel Word64 zero
759+
show : Word64 → String
717760
```
718761

719762
* Added new definition in `Relation.Binary.Construct.Closure.Transitive`

GenerateEverything.hs

+12
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ unsafeModules = map modToFile
4444
, "Codata.Musical.Covec"
4545
, "Codata.Musical.Conversion"
4646
, "Codata.Musical.Stream"
47+
, "Data.Bytestring.Base"
48+
, "Data.Bytestring.Builder.Base"
49+
, "Data.Bytestring.Builder.Primitive"
50+
, "Data.Bytestring.IO"
51+
, "Data.Bytestring.IO.Primitive"
52+
, "Data.Bytestring.Primitive"
53+
, "Data.Word8.Base"
54+
, "Data.Word8.Literals"
55+
, "Data.Word8.Primitive"
56+
, "Data.Word64.Primitive"
57+
, "Data.Word8.Show"
58+
, "Data.Word64.Show"
4759
, "Debug.Trace"
4860
, "Effect.Monad.IO"
4961
, "Effect.Monad.IO.Instances"

src/Data/Bool/Show.agda

+5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
module Data.Bool.Show where
1010

1111
open import Data.Bool.Base using (Bool; false; true)
12+
open import Data.Char.Base using (Char)
1213
open import Data.String.Base using (String)
1314

1415
show : Bool String
1516
show true = "true"
1617
show false = "false"
18+
19+
showBit : Bool Char
20+
showBit true = '1'
21+
showBit false = '0'

src/Data/Bytestring/Base.agda

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Bytestrings: simple types and functions
5+
-- Note that these functions do not perform bound checks.
6+
------------------------------------------------------------------------
7+
8+
{-# OPTIONS --cubical-compatible #-}
9+
10+
module Data.Bytestring.Base where
11+
12+
open import Data.Nat.Base using (ℕ; _+_; _*_; _^_)
13+
open import Agda.Builtin.String using (String)
14+
import Data.Fin.Base as Fin
15+
open import Data.Vec.Base as Vec using (Vec)
16+
open import Data.Word8.Base as Word8 using (Word8)
17+
open import Data.Word64.Base as Word64 using (Word64)
18+
19+
open import Function.Base using (const; _$_)
20+
21+
------------------------------------------------------------------------------
22+
-- Re-export type and operations
23+
24+
open import Data.Bytestring.Primitive as Prim public
25+
using ( Bytestring
26+
; length
27+
; take
28+
; drop
29+
; show
30+
)
31+
renaming (index to getWord8)
32+
33+
------------------------------------------------------------------------------
34+
-- Operations
35+
36+
slice : Word64 Word64 Bytestring Bytestring
37+
slice start chunk buf = take chunk (drop start buf)
38+
39+
------------------------------------------------------------------------------
40+
-- Generic combinators for fixed-size encodings
41+
42+
getWord8s : (n : ℕ) Bytestring Word64 Vec Word8 n
43+
getWord8s n buf idx
44+
= let idx = Word64.toℕ idx in
45+
Vec.map (λ k getWord8 buf (Word64.fromℕ (idx + Fin.toℕ k)))
46+
$ Vec.allFin n
47+
48+
-- Little endian representation:
49+
-- Low place values first
50+
fromWord8sLE : {n w} {W : Set w} (fromℕ : W) Vec Word8 n W
51+
fromWord8sLE f ws = f (Vec.foldr′ (λ w acc Word8.toℕ w + acc * (2 ^ 8)) 0 ws)
52+
53+
-- Big endian representation
54+
-- Big place values first
55+
fromWord8sBE : {n w} {W : Set w} (fromℕ : W) Vec Word8 n W
56+
fromWord8sBE f ws = f (Vec.foldl′ (λ acc w acc * (2 ^ 8) + Word8.toℕ w) 0 ws)
57+
58+
------------------------------------------------------------------------------
59+
-- Decoding to a vector of bytes
60+
61+
toWord8s : (b : Bytestring) Vec Word8 (length b)
62+
toWord8s b = getWord8s _ b (Word64.fromℕ 0)
63+
64+
------------------------------------------------------------------------------
65+
-- Getting Word64
66+
67+
getWord64LE : Bytestring Word64 Word64
68+
getWord64LE buf idx
69+
= fromWord8sLE Word64.fromℕ (getWord8s 8 buf idx)
70+
71+
getWord64BE : Bytestring Word64 Word64
72+
getWord64BE buf idx
73+
= fromWord8sBE Word64.fromℕ (getWord8s 8 buf idx)

src/Data/Bytestring/Builder/Base.agda

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Bytestrings: builder type and functions
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible #-}
8+
9+
module Data.Bytestring.Builder.Base where
10+
11+
open import Data.Nat.Base using (ℕ; zero; suc; _/_; _%_; _^_)
12+
open import Data.Word8.Base as Word8 using (Word8)
13+
open import Data.Word64.Base as Word64 using (Word64)
14+
open import Function.Base using (_∘′_)
15+
16+
------------------------------------------------------------------------------
17+
-- Re-export type and operations
18+
19+
open import Data.Bytestring.Builder.Primitive as Prim public
20+
using ( Builder
21+
; toBytestring
22+
; bytestring
23+
; word8
24+
; empty
25+
; _<>_
26+
)
27+
28+
------------------------------------------------------------------------------
29+
-- High-level combinators
30+
31+
module List where
32+
33+
open import Data.List.Base as List using (List)
34+
35+
concat : List Builder Builder
36+
concat = List.foldr _<>_ empty
37+
38+
module Vec where
39+
40+
open import Data.Vec.Base as Vec using (Vec)
41+
42+
concat : {n} Vec Builder n Builder
43+
concat = Vec.foldr′ _<>_ empty
44+
open Vec
45+
46+
------------------------------------------------------------------------------
47+
-- Generic word-specific combinators
48+
49+
open import Data.Vec.Base as Vec using (Vec; []; _∷_)
50+
51+
wordN : {n} Vec Word8 n Builder
52+
wordN = concat ∘′ Vec.map word8
53+
54+
toWord8sLE : {w} {W : Set w} (n : ℕ) (toℕ : W ℕ) W Vec Word8 n
55+
toWord8sLE n toℕ w = loop (toℕ w) n where
56+
57+
loop : (n : ℕ) Vec Word8 n
58+
loop acc 0 = []
59+
loop acc 1 = Word8.fromℕ acc ∷ []
60+
loop acc (suc n) = Word8.fromℕ (acc % 2 ^ 8) ∷ loop (acc / 2 ^ 8) n
61+
62+
toWord8sBE : {w} {W : Set w} (n : ℕ) (toℕ : W ℕ) W Vec Word8 n
63+
toWord8sBE n toℕ w = Vec.reverse (toWord8sLE n toℕ w)
64+
65+
------------------------------------------------------------------------------
66+
-- Builders for Word64
67+
68+
word64LE : Word64 Builder
69+
word64LE w = wordN (toWord8sLE 8 Word64.toℕ w)
70+
71+
word64BE : Word64 Builder
72+
word64BE w = wordN (toWord8sBE 8 Word64.toℕ w)
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Primitive Bytestrings: builder type and functions
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible #-}
8+
9+
module Data.Bytestring.Builder.Primitive where
10+
11+
open import Agda.Builtin.Nat
12+
open import Agda.Builtin.String
13+
14+
open import Data.Word8.Primitive
15+
open import Data.Bytestring.Primitive using (Bytestring)
16+
17+
infixr 6 _<>_
18+
19+
postulate
20+
-- Builder and execution
21+
Builder : Set
22+
toBytestring : Builder Bytestring
23+
24+
-- Assembling a builder
25+
bytestring : Bytestring Builder
26+
word8 : Word8 Builder
27+
empty : Builder
28+
_<>_ : Builder Builder Builder
29+
30+
{-# FOREIGN GHC import qualified Data.ByteString.Builder as Builder #-}
31+
{-# FOREIGN GHC import qualified Data.ByteString.Lazy as Lazy #-}
32+
{-# FOREIGN GHC import qualified Data.Text as T #-}
33+
{-# COMPILE GHC Builder = type Builder.Builder #-}
34+
{-# COMPILE GHC toBytestring = Lazy.toStrict . Builder.toLazyByteString #-}
35+
{-# COMPILE GHC bytestring = Builder.byteString #-}
36+
{-# COMPILE GHC word8 = Builder.word8 #-}
37+
{-# COMPILE GHC empty = mempty #-}
38+
{-# COMPILE GHC _<>_ = mappend #-}

src/Data/Bytestring/IO.agda

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Bytestrings: IO operations
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --guardedness --cubical-compatible #-}
8+
9+
module Data.Bytestring.IO where
10+
11+
open import Agda.Builtin.String
12+
open import IO using (IO; lift)
13+
open import Data.Bytestring.Base using (Bytestring)
14+
open import Data.Unit.Base using (⊤)
15+
16+
import Data.Bytestring.IO.Primitive as Prim
17+
18+
readFile : String IO Bytestring
19+
readFile fp = lift (Prim.readFile fp)
20+
21+
writeFile : String Bytestring IO ⊤
22+
writeFile fp str = lift (Prim.writeFile fp str)

src/Data/Bytestring/IO/Primitive.agda

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Primitive Bytestrings: IO operations
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --cubical-compatible #-}
8+
9+
module Data.Bytestring.IO.Primitive where
10+
11+
open import Agda.Builtin.String using (String)
12+
open import Agda.Builtin.Unit using (⊤)
13+
open import IO.Primitive.Core using (IO)
14+
15+
open import Data.Bytestring.Primitive
16+
17+
postulate
18+
readFile : String IO Bytestring
19+
writeFile : String Bytestring IO ⊤
20+
21+
{-# FOREIGN GHC import qualified Data.Text as T #-}
22+
{-# FOREIGN GHC import Data.ByteString #-}
23+
{-# COMPILE GHC readFile = Data.ByteString.readFile . T.unpack #-}
24+
{-# COMPILE GHC writeFile = Data.ByteString.writeFile . T.unpack #-}

0 commit comments

Comments
 (0)