File: Class.hs

package info (click to toggle)
haskell-monadrandom 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 116 kB
  • sloc: haskell: 609; makefile: 2
file content (397 lines) | stat: -rw-r--r-- 15,090 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE UndecidableInstances #-}

-- |
-- Module       :  Control.Monad.Random.Class
-- Copyright    :  (c) Brent Yorgey 2016
-- License      :  BSD3 (see LICENSE)
-- Maintainer   :  byorgey@gmail.com
--
-- The 'MonadRandom', 'MonadSplit', and 'MonadInterleave' classes.
--
-- * 'MonadRandom' abstracts over monads with the capability of
--   generating random values.
--
-- * 'MonadSplit' abstracts over random monads with the ability to get a
--   split generator state.  It is not very useful but kept here for
--   backwards compatibility.
--
-- * 'MonadInterleave' abstracts over random monads supporting an
--   'interleave' operation, which allows sequencing computations which do
--   not depend on each other's random generator state, by splitting the
--   generator between them.
--
-- This module also defines convenience functions for sampling from a
-- given collection of values, either uniformly or according to given
-- weights.
module Control.Monad.Random.Class (
  -- * MonadRandom
  MonadRandom (..),

  -- * MonadSplit
  MonadSplit (..),

  -- * MonadInterleave
  MonadInterleave (..),

  -- * Sampling functions
  fromList,
  fromListMay,
  uniform,
  uniformMay,
  weighted,
  weightedMay,
) where

import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Cont
import Control.Monad.Trans.Except
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Maybe
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS
import Control.Monad.Trans.Reader
import qualified Control.Monad.Trans.State.Lazy as LazyState
import qualified Control.Monad.Trans.State.Strict as StrictState
import qualified Control.Monad.Trans.Writer.Lazy as LazyWriter
import qualified Control.Monad.Trans.Writer.Strict as StrictWriter
import qualified Data.Foldable as F
import Data.Word (Word64)
import qualified System.Random as Random

#if MIN_VERSION_base(4,8,0)
#else
import           Data.Monoid                       (Monoid)
#endif

------------------------------------------------------------
-- MonadRandom
------------------------------------------------------------

-- | With a source of random number supply in hand, the 'MonadRandom' class
-- allows the programmer to extract random values of a variety of types.
class Monad m => MonadRandom m where
  -- | Takes a range /(lo,hi)/ and a random number generator
  -- /g/, and returns a computation that returns a random value uniformly
  -- distributed in the closed interval /[lo,hi]/, together with a new
  -- generator. It is unspecified what happens if /lo>hi/. For continuous
  -- types there is no requirement that the values /lo/ and /hi/ are ever
  -- produced, but they may be, depending on the implementation and the
  -- interval.
  --
  -- See 'System.Random.randomR' for details.
  getRandomR :: Random.Random a => (a, a) -> m a

  -- | The same as 'getRandomR', but using a default range determined by the type:
  --
  -- * For bounded types (instances of 'Bounded', such as 'Char'),
  --   the range is normally the whole type.
  --
  -- * For fractional types, the range is normally the semi-closed interval
  -- @[0,1)@.
  --
  -- * For 'Integer', the range is (arbitrarily) the range of 'Int'.
  --
  -- See 'System.Random.random' for details.
  getRandom :: Random.Random a => m a

  -- | Plural variant of 'getRandomR', producing an infinite list of
  -- random values instead of returning a new generator.
  --
  -- See 'System.Random.randomRs' for details.
  getRandomRs :: Random.Random a => (a, a) -> m [a]

  -- | Plural variant of 'getRandom', producing an infinite list of
  -- random values instead of returning a new generator.
  --
  -- See 'System.Random.randoms' for details.
  getRandoms :: Random.Random a => m [a]

instance MonadRandom IO where
  getRandomR = Random.randomRIO
  getRandom = Random.randomIO
  getRandomRs lohi = liftM (Random.randomRs lohi) Random.newStdGen
  getRandoms = liftM Random.randoms Random.newStdGen

instance MonadRandom m => MonadRandom (ContT r m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance MonadRandom m => MonadRandom (ExceptT e m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance MonadRandom m => MonadRandom (IdentityT m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance MonadRandom m => MonadRandom (MaybeT m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance (Monoid w, MonadRandom m) => MonadRandom (LazyRWS.RWST r w s m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance (Monoid w, MonadRandom m) => MonadRandom (StrictRWS.RWST r w s m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance MonadRandom m => MonadRandom (ReaderT r m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance MonadRandom m => MonadRandom (LazyState.StateT s m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance MonadRandom m => MonadRandom (StrictState.StateT s m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance (MonadRandom m, Monoid w) => MonadRandom (LazyWriter.WriterT w m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

instance (MonadRandom m, Monoid w) => MonadRandom (StrictWriter.WriterT w m) where
  getRandomR = lift . getRandomR
  getRandom = lift getRandom
  getRandomRs = lift . getRandomRs
  getRandoms = lift getRandoms

------------------------------------------------------------
-- MonadSplit
------------------------------------------------------------

-- | The class 'MonadSplit' proivides a way to specify a random number
--   generator that can be split into two new generators.
--
--   This class is not very useful in practice: typically, one cannot
--   actually do anything with a generator.  It remains here to avoid
--   breaking existing code unnecessarily.  For a more practically
--   useful interface, see 'MonadInterleave'.
class Monad m => MonadSplit g m | m -> g where
  -- | The 'getSplit' operation allows one to obtain two distinct random number
  -- generators.
  --
  -- See 'System.Random.split' for details.
  getSplit :: m g

instance MonadSplit Random.StdGen IO where
  getSplit = Random.newStdGen

instance MonadSplit g m => MonadSplit g (ContT r m) where
  getSplit = lift getSplit

instance MonadSplit g m => MonadSplit g (ExceptT e m) where
  getSplit = lift getSplit

instance MonadSplit g m => MonadSplit g (IdentityT m) where
  getSplit = lift getSplit

instance MonadSplit g m => MonadSplit g (MaybeT m) where
  getSplit = lift getSplit

instance (Monoid w, MonadSplit g m) => MonadSplit g (LazyRWS.RWST r w s m) where
  getSplit = lift getSplit

instance (Monoid w, MonadSplit g m) => MonadSplit g (StrictRWS.RWST r w s m) where
  getSplit = lift getSplit

instance MonadSplit g m => MonadSplit g (ReaderT r m) where
  getSplit = lift getSplit

instance MonadSplit g m => MonadSplit g (LazyState.StateT s m) where
  getSplit = lift getSplit

instance MonadSplit g m => MonadSplit g (StrictState.StateT s m) where
  getSplit = lift getSplit

instance (Monoid w, MonadSplit g m) => MonadSplit g (LazyWriter.WriterT w m) where
  getSplit = lift getSplit

instance (Monoid w, MonadSplit g m) => MonadSplit g (StrictWriter.WriterT w m) where
  getSplit = lift getSplit

------------------------------------------------------------
-- MonadInterleave
------------------------------------------------------------

-- | The class 'MonadInterleave' proivides a convenient interface atop
--   a 'split' operation on a random generator.
class MonadRandom m => MonadInterleave m where
  -- | If @x :: m a@ is a computation in some random monad, then
  --   @interleave x@ works by splitting the generator, running @x@
  --   using one half, and using the other half as the final generator
  --   state of @interleave x@ (replacing whatever the final generator
  --   state otherwise would have been).  This means that computation
  --   needing random values which comes after @interleave x@ does not
  --   necessarily depend on the computation of @x@.  For example:
  --
  --   > >>> evalRandIO $ snd <$> ((,) <$> undefined <*> getRandom)
  --   > *** Exception: Prelude.undefined
  --   > >>> evalRandIO $ snd <$> ((,) <$> interleave undefined <*> getRandom)
  --   > 6192322188769041625
  --
  --   This can be used, for example, to allow random computations to
  --   run in parallel, or to create lazy infinite structures of
  --   random values.  In the example below, the infinite tree
  --   @randTree@ cannot be evaluated lazily: even though it is cut
  --   off at two levels deep by @hew 2@, the random value in the
  --   right subtree still depends on generation of all the random
  --   values in the (infinite) left subtree, even though they are
  --   ultimately unneeded.  Inserting a call to @interleave@, as in
  --   @randTreeI@, solves the problem: the generator splits at each
  --   @Node@, so random values in the left and right subtrees are
  --   generated independently.
  --
  --   > data Tree = Leaf | Node Int Tree Tree deriving Show
  --   >
  --   > hew :: Int -> Tree -> Tree
  --   > hew 0 _    = Leaf
  --   > hew _ Leaf = Leaf
  --   > hew n (Node x l r) = Node x (hew (n-1) l) (hew (n-1) r)
  --   >
  --   > randTree :: Rand StdGen Tree
  --   > randTree = Node <$> getRandom <*> randTree <*> randTree
  --   >
  --   > randTreeI :: Rand StdGen Tree
  --   > randTreeI = interleave $ Node <$> getRandom <*> randTreeI <*> randTreeI
  --
  --   > >>> hew 2 <$> evalRandIO randTree
  --   > Node 2168685089479838995 (Node (-1040559818952481847) Leaf Leaf) (Node ^CInterrupted.
  --   > >>> hew 2 <$> evalRandIO randTreeI
  --   > Node 8243316398511136358 (Node 4139784028141790719 Leaf Leaf) (Node 4473998613878251948 Leaf Leaf)
  interleave :: m a -> m a

instance MonadInterleave m => MonadInterleave (ContT r m) where
  interleave = mapContT interleave

instance MonadInterleave m => MonadInterleave (ExceptT e m) where
  interleave = mapExceptT interleave

instance MonadInterleave m => MonadInterleave (IdentityT m) where
  interleave = mapIdentityT interleave

instance MonadInterleave m => MonadInterleave (MaybeT m) where
  interleave = mapMaybeT interleave

instance (Monoid w, MonadInterleave m) => MonadInterleave (LazyRWS.RWST r w s m) where
  interleave = LazyRWS.mapRWST interleave

instance (Monoid w, MonadInterleave m) => MonadInterleave (StrictRWS.RWST r w s m) where
  interleave = StrictRWS.mapRWST interleave

instance MonadInterleave m => MonadInterleave (ReaderT r m) where
  interleave = mapReaderT interleave

instance MonadInterleave m => MonadInterleave (LazyState.StateT s m) where
  interleave = LazyState.mapStateT interleave

instance MonadInterleave m => MonadInterleave (StrictState.StateT s m) where
  interleave = StrictState.mapStateT interleave

instance (Monoid w, MonadInterleave m) => MonadInterleave (LazyWriter.WriterT w m) where
  interleave = LazyWriter.mapWriterT interleave

instance (Monoid w, MonadInterleave m) => MonadInterleave (StrictWriter.WriterT w m) where
  interleave = StrictWriter.mapWriterT interleave

------------------------------------------------------------
-- Convenience samplers
------------------------------------------------------------

-- | Sample a random value from a weighted nonempty collection of
--   elements.  Crashes with a call to @error@ if the collection is
--   empty or the total weight is zero.
weighted :: (F.Foldable t, MonadRandom m) => t (a, Rational) -> m a
weighted t = do
  ma <- weightedMay t
  case ma of
    Nothing -> error "Control.Monad.Random.Class.weighted: empty collection, or total weight <= 0"
    Just a -> return a

-- | Sample a random value from a weighted collection of elements.
--   Returns @Nothing@ if the collection is empty or the total weight is
--   zero.
weightedMay :: (F.Foldable t, MonadRandom m) => t (a, Rational) -> m (Maybe a)
weightedMay = fromListMay . F.toList

-- | Sample a random value from a weighted list.  The list must be
--   non-empty and the total weight must be non-zero.
fromList :: MonadRandom m => [(a, Rational)] -> m a
fromList ws = do
  ma <- fromListMay ws
  case ma of
    Nothing -> error "Control.Monad.Random.Class.fromList: empty list, or total weight = 0"
    Just a -> return a

-- | Sample a random value from a weighted list.  Return @Nothing@ if
--   the list is empty or the total weight is nonpositive.
fromListMay :: MonadRandom m => [(a, Rational)] -> m (Maybe a)
fromListMay xs = do
  let s = sum (map snd xs)
      cums = scanl1 (\ ~(_, q) ~(y, s') -> (y, s' + q)) xs
  if s <= 0
    then return Nothing
    else do
      -- Pick a Word64 value uniformly
      w <- getRandom
      -- w / maxBound gives a uniform Rational in the range [0,1].
      -- Subtract from 1 to match the way uniform Double values are
      -- generated, and hence match the old behavior of this function.
      let p = s * (1 - toRational (w :: Word64) / toRational (maxBound :: Word64))
      return . fmap fst . F.find ((>= p) . snd) $ cums

-- | Sample a value uniformly from a nonempty collection of elements.
uniform :: (F.Foldable t, MonadRandom m) => t a -> m a
uniform t = do
  ma <- uniformMay t
  case ma of
    Nothing -> error "Control.Monad.Random.Class.uniform: empty collection"
    Just a -> return a

-- | Sample a value uniformly from a collection of elements.  Return
--   @Nothing@ if the collection is empty.
uniformMay :: (F.Foldable t, MonadRandom m) => t a -> m (Maybe a)
uniformMay = fromListMay . map (flip (,) 1) . F.toList

------------------------------------------------------------

-- The old implementation of `fromListMay`, for comparison.  See
-- https://github.com/byorgey/MonadRandom/issues/53 and
-- https://byorgey.github.io/blog/posts/2024/10/14/MonadRandom-version-bump.html
_fromListMayOld :: MonadRandom m => [(a, Rational)] -> m (Maybe a)
_fromListMayOld xs = do
  let s = fromRational (sum (map snd xs)) :: Double
      cums = scanl1 (\ ~(_, q) ~(y, s') -> (y, s' + q)) xs
  if s <= 0
    then return Nothing
    else do
      p <- liftM toRational $ getRandomR (0, s)
      return . fmap fst . F.find ((>= p) . snd) $ cums