Skip to content

Commit d21d356

Browse files
committed
[Bool] to Seq Bool
1 parent afd9e44 commit d21d356

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

data-bitcode.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ library
4343
, pretty >= 1.1
4444
, bytestring >= 0.10
4545
, binary >= 0.8
46+
, containers >= 0.5
4647
, base16-bytestring
4748
default-language: Haskell2010
4849

@@ -57,6 +58,7 @@ benchmark writer
5758
, pretty >= 1.1
5859
, bytestring >= 0.10
5960
, binary >= 0.8
61+
, containers >= 0.5
6062
, base16-bytestring
6163
, criterion
6264
, data-bitcode

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies:
1414
- pretty >= 1.1
1515
- bytestring >= 0.10
1616
- binary >= 0.8
17+
- containers >= 0.5
1718
- base16-bytestring
1819

1920
library:

src/Data/BitCode/Writer/Monad.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ import Data.ByteString.Base16 (encode)
2323

2424
import Data.BitCode
2525

26+
import Data.Sequence (Seq(..), fromList)
27+
import Data.Foldable (toList)
28+
2629
-- * BitCode
27-
data BitC = BitC { _words :: Int, _bits :: Int, _body :: Bits } deriving (Show)
30+
data BitC = BitC { _words :: Int, _bits :: Int, _body :: Seq Bool } deriving (Show)
2831

2932
instance Monoid BitC where
30-
mempty = BitC 0 0 []
33+
mempty = BitC 0 0 mempty
3134
(BitC w l bs) `mappend` (BitC w' l' bs') = BitC (w+w' + ((l+l') `div` 32)) ((l+l') `mod` 32) (bs `mappend` bs')
3235

3336
-- * BitCode Writer Monad (with access to the current state)
@@ -40,7 +43,7 @@ evalBitCodeWriter :: BitCodeWriter a -> a
4043
evalBitCodeWriter = fst . runBitCodeWriter
4144

4245
execBitCodeWriter :: BitCodeWriter a -> Bits
43-
execBitCodeWriter = _body . snd . runBitCodeWriter
46+
execBitCodeWriter = toList . _body . snd . runBitCodeWriter
4447

4548
class ToBits a where
4649
emit :: a -> BitCodeWriter ()
@@ -74,7 +77,7 @@ instance Monoid (BitCodeWriter ()) where
7477
tell :: Bits -> BitCodeWriter ()
7578
tell bs = BitCode $ \b -> ((), b `mappend` b')
7679
where n = length bs
77-
b' = BitC (n `div` 32) (n `mod` 32) bs
80+
b' = BitC (n `div` 32) (n `mod` 32) (fromList bs)
7881

7982
-- | Get the number of words and bits in the stream.
8083
ask :: BitCodeWriter (Int, Int)
@@ -86,6 +89,7 @@ partition _ [] = []
8689
partition n xs | length xs < n = [xs]
8790
partition n xs | otherwise = h:partition n t
8891
where (h,t) = (take n xs, drop n xs)
92+
8993
toBytes :: Bits -> [Word8]
9094
toBytes = map toFiniteBits . partition 8
9195
where toFiniteBits :: (FiniteBits a) => Bits -> a
@@ -100,7 +104,7 @@ ppBitCodeWriter :: BitCodeWriter () -> PP.Doc
100104
ppBitCodeWriter w = PP.vcat [ PP.text "* Bitcode"
101105
, PP.text "words =" PP.<+> PP.int (_words b)
102106
, PP.text "bits =" PP.<+> PP.int (_bits b)
103-
, PP.text "body =" PP.<+> ppBitsAndBytes (_body b)
107+
, PP.text "body =" PP.<+> ppBitsAndBytes (toList $ _body b)
104108
]
105109
where b = snd (runBitCodeWriter w)
106110
toBitString :: Bits -> String

0 commit comments

Comments
 (0)