Skip to content

Commit 26ba714

Browse files
committed
Stop using liftM and ap in favor of <$> and <*>
GHC versions that could not handle `<$>` and `<*>` without extra `Functor` and `Applicative` constraints are no longer supported.
1 parent c7a34fc commit 26ba714

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

System/Random/MWC.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ module System.Random.MWC
158158
#include "MachDeps.h"
159159
#endif
160160

161-
import Control.Monad (ap, liftM, unless)
161+
import Control.Monad (unless)
162162
import Control.Monad.Primitive (PrimMonad, PrimBase, PrimState, unsafePrimToIO, stToPrim)
163163
import Control.Monad.ST (ST,runST)
164164
import Data.Bits ((.&.), (.|.), shiftL, shiftR, xor)
@@ -310,24 +310,24 @@ instance Variate Word where
310310
{-# INLINE uniformR #-}
311311

312312
instance (Variate a, Variate b) => Variate (a,b) where
313-
uniform g = (,) `liftM` uniform g `ap` uniform g
314-
uniformR ((x1,y1),(x2,y2)) g = (,) `liftM` uniformR (x1,x2) g `ap` uniformR (y1,y2) g
313+
uniform g = (,) <$> uniform g <*> uniform g
314+
uniformR ((x1,y1),(x2,y2)) g = (,) <$> uniformR (x1,x2) g <*> uniformR (y1,y2) g
315315
{-# INLINE uniform #-}
316316
{-# INLINE uniformR #-}
317317

318318
instance (Variate a, Variate b, Variate c) => Variate (a,b,c) where
319-
uniform g = (,,) `liftM` uniform g `ap` uniform g `ap` uniform g
319+
uniform g = (,,) <$> uniform g <*> uniform g <*> uniform g
320320
uniformR ((x1,y1,z1),(x2,y2,z2)) g =
321-
(,,) `liftM` uniformR (x1,x2) g `ap` uniformR (y1,y2) g `ap` uniformR (z1,z2) g
321+
(,,) <$> uniformR (x1,x2) g <*> uniformR (y1,y2) g <*> uniformR (z1,z2) g
322322
{-# INLINE uniform #-}
323323
{-# INLINE uniformR #-}
324324

325325
instance (Variate a, Variate b, Variate c, Variate d) => Variate (a,b,c,d) where
326-
uniform g = (,,,) `liftM` uniform g `ap` uniform g `ap` uniform g
327-
`ap` uniform g
326+
uniform g = (,,,) <$> uniform g <*> uniform g <*> uniform g
327+
<*> uniform g
328328
uniformR ((x1,y1,z1,t1),(x2,y2,z2,t2)) g =
329-
(,,,) `liftM` uniformR (x1,x2) g `ap` uniformR (y1,y2) g `ap`
330-
uniformR (z1,z2) g `ap` uniformR (t1,t2) g
329+
(,,,) <$> uniformR (x1,x2) g <*> uniformR (y1,y2) g <*>
330+
uniformR (z1,z2) g <*> uniformR (t1,t2) g
331331
{-# INLINE uniform #-}
332332
{-# INLINE uniformR #-}
333333

@@ -496,12 +496,12 @@ toSeed v = Seed $ I.create $ do { Gen q <- initialize v; return q }
496496

497497
-- | Save the state of a 'Gen', for later use by 'restore'.
498498
save :: PrimMonad m => Gen (PrimState m) -> m Seed
499-
save (Gen q) = Seed `liftM` G.freeze q
499+
save (Gen q) = Seed <$> G.freeze q
500500
{-# INLINE save #-}
501501

502502
-- | Create a new 'Gen' that mirrors the state of a saved 'Seed'.
503503
restore :: PrimMonad m => Seed -> m (Gen (PrimState m))
504-
restore (Seed s) = Gen `liftM` G.thaw s
504+
restore (Seed s) = Gen <$> G.thaw s
505505
{-# INLINE restore #-}
506506

507507

@@ -591,9 +591,9 @@ aa = 1540315826
591591
uniformWord32 :: PrimMonad m => Gen (PrimState m) -> m Word32
592592
-- NOTE [Carry value]
593593
uniformWord32 (Gen q) = do
594-
i <- nextIndex `liftM` M.unsafeRead q ioff
595-
c <- fromIntegral `liftM` M.unsafeRead q coff
596-
qi <- fromIntegral `liftM` M.unsafeRead q i
594+
i <- nextIndex <$> M.unsafeRead q ioff
595+
c <- fromIntegral <$> M.unsafeRead q coff
596+
qi <- fromIntegral <$> M.unsafeRead q i
597597
let t = aa * qi + c
598598
c' = fromIntegral (t `shiftR` 32)
599599
x = fromIntegral t + c'
@@ -613,11 +613,11 @@ uniform1 f gen = do
613613

614614
uniform2 :: PrimMonad m => (Word32 -> Word32 -> a) -> Gen (PrimState m) -> m a
615615
uniform2 f (Gen q) = do
616-
i <- nextIndex `liftM` M.unsafeRead q ioff
616+
i <- nextIndex <$> M.unsafeRead q ioff
617617
let j = nextIndex i
618-
c <- fromIntegral `liftM` M.unsafeRead q coff
619-
qi <- fromIntegral `liftM` M.unsafeRead q i
620-
qj <- fromIntegral `liftM` M.unsafeRead q j
618+
c <- fromIntegral <$> M.unsafeRead q coff
619+
qi <- fromIntegral <$> M.unsafeRead q i
620+
qj <- fromIntegral <$> M.unsafeRead q j
621621
let t = aa * qi + c
622622
c' = fromIntegral (t `shiftR` 32)
623623
x = fromIntegral t + c'

System/Random/MWC/Distributions.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ module System.Random.MWC.Distributions
4040
) where
4141

4242
import Prelude hiding (mapM)
43-
import Control.Monad (liftM)
4443
import Control.Monad.Primitive (PrimMonad, PrimState)
4544
import Data.Bits ((.&.))
4645
import Data.Foldable (foldl')
@@ -83,7 +82,7 @@ standard :: StatefulGen g m => g -> m Double
8382
standard gen = loop
8483
where
8584
loop = do
86-
u <- (subtract 1 . (*2)) `liftM` uniformDoublePositive01M gen
85+
u <- subtract 1 . (*2) <$> uniformDoublePositive01M gen
8786
ri <- uniformM gen
8887
let i = fromIntegral ((ri :: Word32) .&. 127)
8988
bi = I.unsafeIndex blocks i
@@ -102,8 +101,8 @@ standard gen = loop
102101
else loop
103102
normalTail neg = tailing
104103
where tailing = do
105-
x <- ((/rNorm) . log) `liftM` uniformDoublePositive01M gen
106-
y <- log `liftM` uniformDoublePositive01M gen
104+
x <- (/rNorm) . log <$> uniformDoublePositive01M gen
105+
y <- log <$> uniformDoublePositive01M gen
107106
if y * (-2) < x * x
108107
then tailing
109108
else return $! if neg then x - rNorm else rNorm - x
@@ -257,7 +256,7 @@ bernoulli :: StatefulGen g m
257256
-> g -- ^ Generator
258257
-> m Bool
259258
{-# INLINE bernoulli #-}
260-
bernoulli p gen = (<p) `liftM` uniformDoublePositive01M gen
259+
bernoulli p gen = (< p) <$> uniformDoublePositive01M gen
261260

262261
-- | Random variate generator for categorical distribution.
263262
--
@@ -274,7 +273,7 @@ categorical v gen
274273
| G.null v = pkgError "categorical" "empty weights!"
275274
| otherwise = do
276275
let cv = G.scanl1' (+) v
277-
p <- (G.last cv *) `liftM` uniformDoublePositive01M gen
276+
p <- (G.last cv *) <$> uniformDoublePositive01M gen
278277
return $! case G.findIndex (>=p) cv of
279278
Just i -> i
280279
Nothing -> pkgError "categorical" "bad weights!"

System/Random/MWC/SeedSource.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module System.Random.MWC.SeedSource (
1010
, randomSourceName
1111
) where
1212

13-
import Control.Monad (liftM)
1413
import Data.Word (Word32,Word64)
1514
import Data.Bits (shiftR)
1615
import Data.Ratio ((%), numerator)
@@ -31,8 +30,8 @@ import System.CPUTime (cpuTimePrecision, getCPUTime)
3130
-- Windows system.
3231
acquireSeedTime :: IO [Word32]
3332
acquireSeedTime = do
34-
c <- (numerator . (% cpuTimePrecision)) `liftM` getCPUTime
35-
t <- toRational `liftM` getPOSIXTime
33+
c <- numerator . (% cpuTimePrecision) <$> getCPUTime
34+
t <- toRational <$> getPOSIXTime
3635
let n = fromIntegral (numerator t) :: Word64
3736
return [fromIntegral c, fromIntegral n, fromIntegral (n `shiftR` 32)]
3837

0 commit comments

Comments
 (0)