Skip to content

Commit 688ea35

Browse files
Compatability fixes
1 parent c5bdb2b commit 688ea35

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-31
lines changed

.travis.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ matrix:
1111
env: GHCVER=8.2.2
1212
os: osx
1313

14-
- compiler: "ghc-8.4.4"
15-
env: GHCVER=8.4.4
14+
- compiler: "ghc-8.6.5"
15+
env: GHCVER=8.6.5
1616
os: osx
1717

1818
# Linux builds
@@ -53,14 +53,15 @@ matrix:
5353
addons: {apt: {packages: [cabal-install-head, ghc-head], sources: [hvr-ghc]}}
5454

5555
allow_failures:
56+
- compiler: "ghc-8.8.1"
5657
- compiler: "ghc-head"
5758

5859
before_install:
5960
# MacOS setups
6061
# Install specific GHC version
6162
- if [[ "$TRAVIS_OS_NAME" == "osx" && "$GHCVER" == "8.0.2" ]]; then brew install [email protected] cabal-install && export PATH=/usr/local/opt/[email protected]/bin:$PATH; fi
6263
- if [[ "$TRAVIS_OS_NAME" == "osx" && "$GHCVER" == "8.2.2" ]]; then brew install [email protected] cabal-install && export PATH=/usr/local/opt/[email protected]/bin:$PATH; fi
63-
- if [[ "$TRAVIS_OS_NAME" == "osx" && "$GHCVER" == "8.4.4" ]]; then brew install ghc cabal-install && export PATH=/usr/local/opt/ghc/bin:$PATH; fi
64+
- if [[ "$TRAVIS_OS_NAME" == "osx" && "$GHCVER" == "8.6.5" ]]; then brew install ghc cabal-install && export PATH=/usr/local/opt/ghc/bin:$PATH; fi
6465

6566
# Linux setups initial setup
6667
# Export PATH to specific GHC & Cabal version specific programs
@@ -95,7 +96,12 @@ install:
9596
fi
9697
9798
# Install dependencies and utility programs
98-
- cabal install --enable-benchmarks --enable-documentation --enable-tests --only-dependencies
99+
- |
100+
if $GHCHEAD; then
101+
cabal install --enable-benchmarks --enable-documentation --enable-tests --only-dependencies --allow-newer
102+
else
103+
cabal install --enable-benchmarks --enable-documentation --enable-tests --only-dependencies
104+
fi
99105
- cabal install hpc
100106

101107
script:

bv-little.cabal

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ library
5151

5252
if !impl(ghc >= 8.0)
5353

54-
build-depends: semigroups
54+
build-depends: semigroups >= 0.18 && < 0.19
5555

5656
default-language: Haskell2010
5757

@@ -104,7 +104,7 @@ Test-Suite test-suite
104104

105105
main-is: TestSuite.hs
106106

107-
build-depends: base >= 4.5.1 && < 4.13
107+
build-depends: base >= 4.5.1 && < 5
108108
, bv-little
109109
, deepseq
110110
, hashable
@@ -120,7 +120,7 @@ Test-Suite test-suite
120120

121121
if !impl(ghc >= 8.0)
122122

123-
build-depends: semigroups
123+
build-depends: semigroups >= 0.18 && < 0.19
124124
, transformers
125125

126126
default-language: Haskell2010
@@ -139,7 +139,7 @@ benchmark benchmark-suite
139139

140140
main-is: Benchmarks.hs
141141

142-
build-depends: base >= 4.5.1 && < 4.13
142+
build-depends: base >= 4.5.1 && < 5
143143
, bv-little
144144
, criterion
145145
, deepseq
@@ -150,7 +150,7 @@ benchmark benchmark-suite
150150

151151
if !impl(ghc >= 8.0)
152152

153-
build-depends: semigroups
153+
build-depends: semigroups >= 0.18 && < 0.19
154154

155155
default-language: Haskell2010
156156

src/Data/BitVector/LittleEndian.hs

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ module Data.BitVector.LittleEndian
6464
, dimension
6565
, isZeroVector
6666
, subRange
67-
, showNatural
6867
) where
6968

7069

@@ -88,7 +87,7 @@ import GHC.Generics
8887
import GHC.Integer.GMP.Internals
8988
import GHC.Integer.Logarithms
9089
import GHC.Natural
91-
import Test.QuickCheck (Arbitrary(..), CoArbitrary(..), NonNegative(..), suchThat, variant)
90+
import Test.QuickCheck (Arbitrary(..), CoArbitrary(..), NonNegative(..), choose, suchThat, variant)
9291
import TextShow (TextShow(showb))
9392

9493

@@ -118,11 +117,50 @@ type instance MonoKey BitVector = Word
118117
-- @since 0.1.0
119118
instance Arbitrary BitVector where
120119

120+
-- Arbitrary instance distribution weighting:
121+
-- - 2% = (maxBound :: Word)
122+
-- - 2% = (maxBound :: Word) + 1
123+
-- - 8% = all bits on
124+
-- - 8% = all bits off
125+
-- - 80% = any bit configuration
121126
arbitrary = do
122-
dimVal <- getNonNegative <$> arbitrary
123-
let upperBound = shiftL 1 dimVal
124-
intVal <- (getNonNegative <$> arbitrary) `suchThat` (< upperBound)
125-
pure . BV (toEnum dimVal) $ intToNat intVal
127+
-- 1/25 chance of generating the boundary value at which the natural number
128+
-- must use different Natural constructors: NatS# & NatJ#
129+
n <- choose (0, 25 :: Word)
130+
case n of
131+
0 -> boundaryValue
132+
1 -> allBitsOn
133+
2 -> allBitsOn
134+
3 -> allBitsOff
135+
4 -> allBitsOff
136+
_ -> anyBitValue
137+
where
138+
allBitsOn = genBitVector $ Just True
139+
allBitsOff = genBitVector $ Just False
140+
anyBitValue = genBitVector $ Nothing
141+
142+
boundaryValue = do
143+
let wrdVal = maxBound :: Word
144+
let dimVal = toEnum $ popCount wrdVal
145+
let numVal = wordToNatural wrdVal
146+
-- 50/50 change to generate above or below the constructor boundary
147+
underBoundary <- arbitrary
148+
let (lowerBound, naturalVal)
149+
| underBoundary = (dimVal , numVal )
150+
| otherwise = (dimVal + 1, numVal + 1)
151+
widthVal <- (getNonNegative <$> arbitrary) `suchThat` (>= lowerBound)
152+
pure $ BV widthVal naturalVal
153+
154+
genBitVector spec = do
155+
dimVal <- getNonNegative <$> arbitrary
156+
let upperBound = shiftL 1 dimVal
157+
-- 1/5 chance all bits on or all bits off
158+
natVal <- case spec of
159+
Just False -> pure $ intToNat 0
160+
Just True -> pure . intToNat $ upperBound - 1
161+
Nothing -> fmap intToNat $
162+
(getNonNegative <$> arbitrary) `suchThat` (< upperBound)
163+
pure $ BV (toEnum dimVal) natVal
126164

127165

128166
-- |
@@ -158,14 +196,14 @@ instance Bits BitVector where
158196
in BV w $ n .&. mask
159197

160198
{-# INLINE setBit #-}
161-
setBit bv@(BV w n) i@(I# v)
199+
setBit bv@(BV w n) i
162200
| i < 0 = bv
163-
| otherwise = BV (max w j) $ (n `orNatural` (bitNatural v :: Natural) :: Natural)
201+
| otherwise = BV (max w j) $ n `setBit` i
164202
where
165203
!j = toEnum i + 1
166204

167205
{-# INLINE testBit #-}
168-
testBit (BV w n) i = i >= 0 && toEnum i < w && n `testBitNatural` i
206+
testBit (BV w n) i = i >= 0 && toEnum i < w && n `testBit` i
169207

170208
bitSize (BV w _) = fromEnum w
171209

@@ -1116,16 +1154,9 @@ toInt w
11161154
-- this function does not throw an exception when an negative valued 'Integer'
11171155
-- is supplied and is also compatible with base < 4.10.0.0.
11181156
{-# INLINE intToNat #-}
1157+
-- {-# NOINLINE intToNat #-}
11191158
intToNat :: Integer -> Natural
1120-
intToNat (S# i#) | I# i# >= 0 = NatS# (int2Word# i#)
1121-
intToNat (Jp# bn) = NatJ# bn
1122-
intToNat _ = NatS# (int2Word# 0#)
1123-
1124-
1125-
-- |
1126-
-- Utility Function for printing the 'Natural' number constructor.
1127-
showNatural :: BitVector -> String
1128-
showNatural (BV w (NatS# v)) = unwords ["["<>show w<>"]", "NatS#", show (W# v)]
1129-
showNatural (BV w n@(NatJ# _)) = unwords ["["<>show w<>"]", "NatJ#", show n]
1130-
1131-
1159+
intToNat (S# i#) | isTrue# (i# >=# 0#) = NatS# (int2Word# i#)
1160+
intToNat (Jp# bn) | isTrue# (sizeofBigNat# bn ==# 1#) = NatS# (bigNatToWord bn)
1161+
| otherwise = NatJ# bn
1162+
intToNat _ = NatS# (int2Word# 0#)

test/TestSuite.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import TextShow (TextShow(showb), toString)
3434

3535
infix 0 -=>
3636
(-=>) :: QC.Testable p => Bool -> p -> Property
37-
(-=>) p q = (not p) .||. q
37+
(-=>) p q = not p .||. q
3838

3939

4040
main :: IO ()

0 commit comments

Comments
 (0)