Skip to content

Commit c30a073

Browse files
committed
Add support for random-1.3
1 parent 08dfb00 commit c30a073

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

cuddle.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ library
8181
ordered-containers ^>=0.2.4,
8282
parser-combinators ^>=1.3,
8383
prettyprinter ^>=1.7.1,
84-
random <1.3,
84+
random >=1.2,
8585
regex-tdfa ^>=1.3.2,
8686
scientific ^>=0.3.8,
8787
text >=2.0.2 && <2.2,

src/Codec/CBOR/Cuddle/CBOR/Gen.hs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
{-# LANGUAGE ScopedTypeVariables #-}
1010
{-# LANGUAGE ViewPatterns #-}
1111

12+
#if MIN_VERSION_random(1,3,0)
13+
{-# OPTIONS_GHC -Wno-deprecations #-} -- Due to usage of `split`
14+
#endif
1215
-- | Generate example CBOR given a CDDL specification
1316
module Codec.CBOR.Cuddle.CBOR.Gen (generateCBORTerm, generateCBORTerm') where
1417

15-
import qualified Control.Monad.State.Strict as MTL
1618
import Capability.Reader
1719
import Capability.Sink (HasSink)
1820
import Capability.Source (HasSource, MonadState (..))
@@ -34,6 +36,7 @@ import Codec.CBOR.Write qualified as CBOR
3436
import Control.Monad (join, replicateM, (<=<))
3537
import Control.Monad.Reader (Reader, runReader)
3638
import Control.Monad.State.Strict (StateT, runStateT)
39+
import Control.Monad.State.Strict qualified as MTL
3740
import Data.Bifunctor (second)
3841
import Data.ByteString (ByteString)
3942
import Data.ByteString.Base16 qualified as Base16
@@ -54,6 +57,11 @@ import System.Random.Stateful (
5457
randomM,
5558
uniformByteStringM,
5659
)
60+
#if MIN_VERSION_random(1,3,0)
61+
import System.Random.Stateful (
62+
SplitGen (..)
63+
)
64+
#endif
5765

5866
--------------------------------------------------------------------------------
5967
-- Generator infrastructure
@@ -81,9 +89,17 @@ instance RandomGen g => RandomGen (GenState g) where
8189
genWord16 = withRandomSeed genWord16
8290
genWord32 = withRandomSeed genWord32
8391
genWord64 = withRandomSeed genWord64
84-
split s =
85-
case split (randomSeed s) of
86-
(gen', gen) -> (s {randomSeed = gen'}, s {randomSeed = gen})
92+
split = splitGenStateWith split
93+
94+
#if MIN_VERSION_random(1,3,0)
95+
instance SplitGen g => SplitGen (GenState g) where
96+
splitGen = splitGenStateWith splitGen
97+
#endif
98+
99+
splitGenStateWith :: (g -> (g, g)) -> GenState g -> (GenState g, GenState g)
100+
splitGenStateWith f s =
101+
case f (randomSeed s) of
102+
(gen', gen) -> (s {randomSeed = gen'}, s {randomSeed = gen})
87103

88104
withRandomSeed :: (t -> (a, g)) -> GenState t -> (a, GenState g)
89105
withRandomSeed f s =

0 commit comments

Comments
 (0)