9
9
{-# LANGUAGE ScopedTypeVariables #-}
10
10
{-# LANGUAGE ViewPatterns #-}
11
11
12
+ #if MIN_VERSION_random(1,3,0)
13
+ {-# OPTIONS_GHC -Wno-deprecations #-} -- Due to usage of `split`
14
+ #endif
12
15
-- | Generate example CBOR given a CDDL specification
13
16
module Codec.CBOR.Cuddle.CBOR.Gen (generateCBORTerm , generateCBORTerm' ) where
14
17
15
- import qualified Control.Monad.State.Strict as MTL
16
18
import Capability.Reader
17
19
import Capability.Sink (HasSink )
18
20
import Capability.Source (HasSource , MonadState (.. ))
@@ -34,6 +36,7 @@ import Codec.CBOR.Write qualified as CBOR
34
36
import Control.Monad (join , replicateM , (<=<) )
35
37
import Control.Monad.Reader (Reader , runReader )
36
38
import Control.Monad.State.Strict (StateT , runStateT )
39
+ import Control.Monad.State.Strict qualified as MTL
37
40
import Data.Bifunctor (second )
38
41
import Data.ByteString (ByteString )
39
42
import Data.ByteString.Base16 qualified as Base16
@@ -54,6 +57,11 @@ import System.Random.Stateful (
54
57
randomM ,
55
58
uniformByteStringM ,
56
59
)
60
+ #if MIN_VERSION_random(1,3,0)
61
+ import System.Random.Stateful (
62
+ SplitGen (.. )
63
+ )
64
+ #endif
57
65
58
66
--------------------------------------------------------------------------------
59
67
-- Generator infrastructure
@@ -81,9 +89,17 @@ instance RandomGen g => RandomGen (GenState g) where
81
89
genWord16 = withRandomSeed genWord16
82
90
genWord32 = withRandomSeed genWord32
83
91
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})
87
103
88
104
withRandomSeed :: (t -> (a , g )) -> GenState t -> (a , GenState g )
89
105
withRandomSeed f s =
0 commit comments