ModuleMonadRandom-0.6.2.1Haskell2010
Control.Monad.Random.Lazy
Random monads that are lazy in the generator state. For a strict version, see Control.Monad.Random.Strict, which has the same interface.
- 4 types
- 14 classes
- 76 values
- PackageMonadRandom-0.6.2.1
- Exports97
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceLazy.hs
The Rand monad
8 declarationsliftRand :: (g -> (a, g))pure random transformer
-> Rand g aequivalent generator-passing computation
Construct a random monad computation from a function. (The inverse of runRand.)
runRand :: Rand g agenerator-passing computation to execute
-> ginitial generator
-> (a, g)return value and final generator
Unwrap a random monad computation as a function. (The inverse of liftRand.)
Evaluate a random computation in the IO monad, splitting the global standard generator to get a new one for the computation.
The RandT monad transformer
8 declarationsInstances22MonadRWS, MonadError, MonadReader, MonadState, MonadWriter, MonadSplit, …
(MonadReader r m, MonadWriter w m, MonadState s m) => MonadRWS r w s (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadError e m => MonadError e (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadReader r m => MonadReader r (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadState s m => MonadState s (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadWriter w m => MonadWriter w (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy(RandomGen g, Monad m) => MonadSplit g (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadTrans (RandT g)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => RandomGenM (RandGen g) g (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => StatefulGen (RandGen g) (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonad m => Monad (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyFunctor m => Functor (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadFix m => MonadFix (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadFail m => MonadFail (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonad m => Applicative (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadPlus m => Alternative (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadPlus m => MonadPlus (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadIO m => MonadIO (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadCont m => MonadCont (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyPrimMonad m => PrimMonad (RandT s m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy(Monad m, RandomGen g) => MonadInterleave (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy(RandomGen g, Monad m) => MonadRandom (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazytype PrimState (RandT s m) = PrimState mDefined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.Lazy
liftRandT :: (g -> m (a, g))impure random transformer
-> RandT g m aequivalent generator-passing computation
Construct a random monad computation from an impure function. (The inverse of runRandT.)
runRandT :: RandT g m agenerator-passing computation to execute
-> ginitial generator
-> m (a, g)return value and final generator
Unwrap a random monad computation as an impure function. (The inverse of liftRandT.)
Evaluate a random computation that is embedded in the IO monad, splitting the global standard generator to get a new one for the computation.
Some convenience re-exports
81 declarationsDeprecated. In favor of uniformByteString
Generates a ByteString of the specified size using a pure pseudo-random
number generator. See uniformByteStringM for the monadic version.
Examples
import System.Randomimport Data.ByteStringlet pureGen = mkStdGen 137:seti -Wno-deprecationsunpack . fst . genByteString 10 $ pureGen[51,123,251,37,49,167,90,109,1,4]
Gets the global pseudo-random number generator. Extracts the contents of globalStdGen
Uses the supplied function to get a value from the current global
random generator, and updates the global generator with the new generator
returned by the function. For example, rollDice produces a pseudo-random integer
between 1 and 6:
rollDice = getStdRandom (randomR (1, 6))replicateM 10 (rollDice :: IO Int)[1,1,1,4,5,6,1,2,2,5]
This is an outdated function and it is recommended to switch to its equivalent applyAtomicGen instead, possibly with the globalStdGen if relying on the global state is acceptable.
import System.Random.StatefulrollDice = applyAtomicGen (uniformR (1, 6)) globalStdGenreplicateM 10 (rollDice :: IO Int)[2,1,1,5,4,3,6,6,3,2]
Initialize StdGen using system entropy (i.e. /dev/urandom) when it is
available, while falling back on using system time as the seed.
Applies split to the current global pseudo-random generator globalStdGen, updates it with one of the results, and returns the other.
A variant of randomM that uses the global pseudo-random number generator globalStdGen.
import Data.IntrandomIO :: IO Int32114794456
This function is equivalent to getStdRandom random and is included in
this interface for historical reasons and backwards compatibility. It is
recommended to use uniformM instead, possibly with
the globalStdGen if relying on the global state is
acceptable.
import System.Random.StatefuluniformM globalStdGen :: IO Int32-1768545016
A variant of randomRM that uses the global pseudo-random number generator globalStdGen
randomRIO (2020, 2100) :: IO Int2028
Similar to randomIO, this function is equivalent to getStdRandom
randomR and is included in this interface for historical reasons and
backwards compatibility. It is recommended to use
uniformRM instead, possibly with the
globalStdGen if relying on the global state is
acceptable.
import System.Random.StatefuluniformRM (2020, 2100) globalStdGen :: IO Int2044
Sets the global pseudo-random number generator. Overwrites the contents of globalStdGen
Generates a ByteString of the specified size using a pure pseudo-random
number generator. See uniformByteStringM for the monadic version.
Examples
import System.Randomimport Data.ByteString (unpack)let pureGen = mkStdGen 137unpack . fst $ uniformByteString 10 pureGen[51,123,251,37,49,167,90,109,1,4]
uniformFillMutableByteArray :: RandomGen g=> MutableByteArray sMutable array to fill with random bytes
-> IntOffset into a mutable array from the beginning in number of bytes. Offset will be clamped into the range between 0 and the total size of the mutable array
-> IntNumber of randomly generated bytes to write into the array. This number will be clamped between 0 and the total size of the array without the offset.
-> g-> ST s g
Fill in a slice of a mutable byte array with randomly generated bytes. This function does not fail, instead it clamps the offset and number of bytes to generate into a valid range.
Produce a list of the supplied length with elements generated uniformly.
See uniformListM for a stateful counterpart.
Examples
let gen = mkStdGen 2023import Data.Word (Word16)uniformList 5 gen :: ([Word16], StdGen)([56342,15850,25292,14347,13919],StdGen {unStdGen = SMGen 6446154349414395371 1920468677557965761})
Produce a list of the supplied length with elements generated uniformly.
See uniformListM for a stateful counterpart.
Examples
let gen = mkStdGen 2023uniformListR 10 (20, 30) gen :: ([Int], StdGen)([26,30,27,24,30,25,27,21,27,27],StdGen {unStdGen = SMGen 12965503083958398648 1920468677557965761})
Produce an infinite list of pseudo-random values in a specified range. Same as uniforms, integrates nicely with list fusion. There is no way to recover the final generator, therefore either use split before calling uniformRs or use uniformListR instead.
Similar to randomRs, except it relies on UniformRange type class instead of Random.
Examples
let gen = mkStdGen 2023take 5 $ uniformRs (10, 100) gen :: [Int][32,86,21,57,39]
Same as uniformByteArray False, but for ShortByteString.
Returns a ShortByteString of length n filled with pseudo-random bytes.
Examples
import System.Randomimport Data.ByteString.Short (unpack)let pureGen = mkStdGen 137unpack . fst $ uniformShortByteString 10 pureGen[51,123,251,37,49,167,90,109,1,4]
Shuffle elements of a list in a uniformly random order.
Examples
uniformShuffleList "ELVIS" $ mkStdGen 252("LIVES",StdGen {unStdGen = SMGen 17676540583805057877 5302934877338729551})
Produce an infinite list of pseudo-random values. Integrates nicely with list fusion. Naturally, there is no way to recover the final generator, therefore either use split before calling uniforms or use uniformList instead.
Similar to randoms, except it relies on Uniform type class instead of Random
Examples
let gen = mkStdGen 2023import Data.Word (Word16)take 5 $ uniforms gen :: [Word16][56342,15850,25292,14347,13919]
Constructs a StdGen deterministically from an Int seed. See mkStdGen64 for a Word64 variant that is architecture agnostic.
Constructs a StdGen deterministically from a Word64 seed.
The difference between mkStdGen is that mkStdGen64 will work the same on 64-bit and 32-bit architectures, while the former can only use 32-bit of information for initializing the psuedo-random number generator on 32-bit operating systems
uniformByteArray Efficiently generates a sequence of pseudo-random bytes in a platform independent manner.
Just like mkSeed, but uses ByteString as argument. Results in a memcopy of the seed.
Convert a Seed to a list of 64bit words.
Construct a seed from a list of 64-bit words. At most SeedSize many bytes will be used.
This is a function that shows the name of the generator type, which is useful for error reporting.
Get the expected size of the Seed in number bytes
Just like seedSize, except it accepts a proxy as an argument.
Just like unSeed, but produced a ByteString. Results in a memcopy of the seed.
Helper function that allows for operating directly on the Seed, while supplying a function that uses the pseudo-random number generator that is constructed from that Seed.
Example
:set -XTypeApplicationsimport System.RandomwithSeed (nonEmptyToSeed (pure 2024) :: Seed StdGen) (uniform @Int)(1039666877624726199,Seed [0xe9, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
Read the seed from a file and use it for constructing a pseudo-random number generator. After supplied action has been applied to the constructed generator, the resulting generator will be converted back to a seed and written to the same file.
Same as withSeed, except it is useful with monadic computation and frozen generators.
See withSeedMutableGen for a helper that also handles seeds for mutable pseduo-random number generators.
The class of types for which random values can be generated. Most instances of Random will produce values that are uniformly distributed on the full range, but for those types without a well-defined "full range" some sensible default subrange will be selected.
Random exists primarily for backwards compatibility with version 1.1 of this library. In new code, use the better specified Uniform and UniformRange instead.
Methods
randomR :: RandomGen g => (a, a) -> g -> (a, g)Takes a range (lo,hi) and a pseudo-random number generator g, and returns a pseudo-random value uniformly distributed over the closed interval [lo,hi], together with a new generator. It is unspecified what happens if lo>hi, but usually the values will simply get swapped.
Example3 expressions let gen = mkStdGen 26fst $ randomR ('a', 'z') gen'z'fst $ randomR ('a', 'z') gen'z'
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.
There is no requirement to follow the
Ordinstance and the concept of range can be defined on per type basis. For example product types will treat their values independently:Example1 expression fst $ randomR (('a', 5.0), ('z', 10.0)) $ mkStdGen 26('z',5.22694980853051)
In case when a lawful range is desired uniformR should be used instead.
random :: RandomGen g => g -> (a, g)randomRs :: RandomGen g => (a, a) -> g -> [a]Plural variant of randomR, producing an infinite list of pseudo-random values instead of returning a new generator.
randoms :: RandomGen g => g -> [a]Plural variant of random, producing an infinite list of pseudo-random values instead of returning a new generator.
Instances43Random, …
Random IntegerDefined in random-1.3.1 · System.RandomRandom CBoolDefined in random-1.3.1 · System.RandomRandom CCharDefined in random-1.3.1 · System.RandomRandom CDoubleDefined in random-1.3.1 · System.RandomNote - random produces values in the closed range
[0,1].Random CFloatDefined in random-1.3.1 · System.RandomNote - random produces values in the closed range
[0,1].Random CIntDefined in random-1.3.1 · System.RandomRandom CIntMaxDefined in random-1.3.1 · System.RandomRandom CIntPtrDefined in random-1.3.1 · System.RandomRandom CLLongDefined in random-1.3.1 · System.RandomRandom CLongDefined in random-1.3.1 · System.RandomRandom CPtrdiffDefined in random-1.3.1 · System.RandomRandom CSCharDefined in random-1.3.1 · System.RandomRandom CShortDefined in random-1.3.1 · System.RandomRandom CSigAtomicDefined in random-1.3.1 · System.RandomRandom CSizeDefined in random-1.3.1 · System.RandomRandom CUCharDefined in random-1.3.1 · System.RandomRandom CUIntDefined in random-1.3.1 · System.RandomRandom CUIntMaxDefined in random-1.3.1 · System.RandomRandom CUIntPtrDefined in random-1.3.1 · System.RandomRandom CULLongDefined in random-1.3.1 · System.RandomRandom CULongDefined in random-1.3.1 · System.RandomRandom CUShortDefined in random-1.3.1 · System.RandomRandom CWcharDefined in random-1.3.1 · System.RandomRandom Int16Defined in random-1.3.1 · System.RandomRandom Int32Defined in random-1.3.1 · System.RandomRandom Int64Defined in random-1.3.1 · System.RandomRandom Int8Defined in random-1.3.1 · System.RandomRandom BoolDefined in random-1.3.1 · System.RandomRandom CharDefined in random-1.3.1 · System.RandomRandom DoubleDefined in random-1.3.1 · System.RandomNote - random produces values in the closed range
[0,1].Random FloatDefined in random-1.3.1 · System.RandomNote - random produces values in the closed range
[0,1].Random IntDefined in random-1.3.1 · System.RandomRandom WordDefined in random-1.3.1 · System.RandomRandom Word16Defined in random-1.3.1 · System.RandomRandom Word32Defined in random-1.3.1 · System.RandomRandom Word64Defined in random-1.3.1 · System.RandomRandom Word8Defined in random-1.3.1 · System.Random(Random a, Random b) => Random (a, b)Defined in random-1.3.1 · System.RandomNote - randomR treats
aandbtypes independently(Random a, Random b, Random c) => Random (a, b, c)Defined in random-1.3.1 · System.RandomNote - randomR treats
a,bandctypes independently(Random a, Random b, Random c, Random d) => Random (a, b, c, d)Defined in random-1.3.1 · System.RandomNote - randomR treats
a,b,canddtypes independently(Random a, Random b, Random c, Random d, Random e) => Random (a, b, c, d, e)Defined in random-1.3.1 · System.RandomNote - randomR treats
a,b,c,dandetypes independently(Random a, Random b, Random c, Random d, Random e, Random f) => Random (a, b, c, d, e, f)Defined in random-1.3.1 · System.RandomNote - randomR treats
a,b,c,d,eandftypes independently(Random a, Random b, Random c, Random d, Random e, Random f, Random g) => Random (a, b, c, d, e, f, g)Defined in random-1.3.1 · System.RandomNote - randomR treats
a,b,c,d,e,fandgtypes independently
A type class for data with a finite number of inhabitants. This type class
is used in the default implementation of System.Random.Stateful.Uniform.
Users are not supposed to write instances of Finite manually. There is a default implementation in terms of Generic instead.
:seti -XDeriveGeneric -XDeriveAnyClassimport GHC.Generics (Generic)data MyBool = MyTrue | MyFalse deriving (Generic, Finite)data Action = Code MyBool | Eat (Maybe Bool) | Sleep deriving (Generic, Finite)
Instances23Finite, …
Finite VoidDefined in random-1.3.1 · System.Random.GFiniteFinite Int16Defined in random-1.3.1 · System.Random.GFiniteFinite Int32Defined in random-1.3.1 · System.Random.GFiniteFinite Int64Defined in random-1.3.1 · System.Random.GFiniteFinite Int8Defined in random-1.3.1 · System.Random.GFiniteFinite BoolDefined in random-1.3.1 · System.Random.GFiniteFinite CharDefined in random-1.3.1 · System.Random.GFiniteFinite IntDefined in random-1.3.1 · System.Random.GFiniteFinite OrderingDefined in random-1.3.1 · System.Random.GFiniteFinite WordDefined in random-1.3.1 · System.Random.GFiniteFinite Word16Defined in random-1.3.1 · System.Random.GFiniteFinite Word32Defined in random-1.3.1 · System.Random.GFiniteFinite Word64Defined in random-1.3.1 · System.Random.GFiniteFinite Word8Defined in random-1.3.1 · System.Random.GFiniteFinite ()Defined in random-1.3.1 · System.Random.GFiniteFinite a => Finite (Maybe a)Defined in random-1.3.1 · System.Random.GFinite(Finite a, Finite b) => Finite (Either a b)Defined in random-1.3.1 · System.Random.GFinite(Finite a, Finite b) => Finite (a, b)Defined in random-1.3.1 · System.Random.GFinite(Finite a, Finite b, Finite c) => Finite (a, b, c)Defined in random-1.3.1 · System.Random.GFinite(Finite a, Finite b, Finite c, Finite d) => Finite (a, b, c, d)Defined in random-1.3.1 · System.Random.GFinite(Finite a, Finite b, Finite c, Finite d, Finite e) => Finite (a, b, c, d, e)Defined in random-1.3.1 · System.Random.GFinite(Finite a, Finite b, Finite c, Finite d, Finite e, Finite f) => Finite (a, b, c, d, e, f)Defined in random-1.3.1 · System.Random.GFinite(Finite a, Finite b, Finite c, Finite d, Finite e, Finite f, Finite g) => Finite (a, b, c, d, e, f, g)Defined in random-1.3.1 · System.Random.GFinite
RandomGen is an interface to pure pseudo-random number generators.
StdGen is the standard RandomGen instance provided by this library.
Methods
next :: g -> (Int, g)genWord8 :: g -> (Word8, g)genWord16 :: g -> (Word16, g)genWord32 :: g -> (Word32, g)genWord64 :: g -> (Word64, g)genWord32R :: Word32 -> g -> (Word32, g)genWord32R upperBound greturns a Word32 that is uniformly distributed over the range[0, upperBound].genWord64R :: Word64 -> g -> (Word64, g)genWord64R upperBound greturns a Word64 that is uniformly distributed over the range[0, upperBound].genShortByteString :: Int -> g -> (ShortByteString, g)Same as
uniformByteArray False, but for ShortByteString.genShortByteString n greturns a ShortByteString of lengthnfilled with pseudo-random bytes.Note - This function will be removed from the type class in the next major release as it is no longer needed because of unsafeUniformFillMutableByteArray.
unsafeUniformFillMutableByteArray :: MutableByteArray s -> Int -> Int -> g -> ST s gFill in the supplied MutableByteArray with uniformly generated random bytes. This function is unsafe because it is not required to do any bounds checking. For a safe variant use
System.Random.Sateful.uniformFillMutableByteArrayMinstead.Default type class implementation uses defaultUnsafeUniformFillMutableByteArray.
genRange :: g -> (Int, Int)split :: g -> (g, g)
Instances8RandomGen, …
RandomGen StdGenDefined in random-1.3.1 · System.Random.InternalRandomGen SMGenDefined in random-1.3.1 · System.Random.InternalRandomGen SMGenDefined in random-1.3.1 · System.Random.InternalRandomGen g => RandomGen (StateGen g)Defined in random-1.3.1 · System.Random.InternalRandomGen g => RandomGen (AtomicGen g)Defined in random-1.3.1 · System.Random.StatefulRandomGen g => RandomGen (IOGen g)Defined in random-1.3.1 · System.Random.StatefulRandomGen g => RandomGen (STGen g)Defined in random-1.3.1 · System.Random.StatefulRandomGen g => RandomGen (TGen g)Defined in random-1.3.1 · System.Random.Stateful
This is a binary form of pseudo-random number generator's state. It is designed to be safe and easy to use for input/output operations like restoring from file, transmitting over the network, etc.
Constructor is not exported, becasue it is important for implementation to enforce the invariant of the underlying byte array being of the exact same length as the generator has specified in SeedSize. Use mkSeed and unSeed to get access to the raw bytes in a safe manner.
Pseudo-random generators that can be split into two separate and independent psuedo-random generators should provide an instance for this type class.
Historically this functionality was included in the RandomGen type class in the split function, however, few pseudo-random generators possess this property of splittability. This lead the old split function being usually implemented in terms of error.
Methods
splitGen :: g -> (g, g)Returns two distinct pseudo-random number generators.
Implementations should take care to ensure that the resulting generators are not correlated.
Instances8SplitGen, …
SplitGen StdGenDefined in random-1.3.1 · System.Random.InternalSplitGen SMGenDefined in random-1.3.1 · System.Random.InternalSplitGen SMGenDefined in random-1.3.1 · System.Random.InternalSplitGen g => SplitGen (StateGen g)Defined in random-1.3.1 · System.Random.InternalSplitGen g => SplitGen (AtomicGen g)Defined in random-1.3.1 · System.Random.StatefulSplitGen g => SplitGen (IOGen g)Defined in random-1.3.1 · System.Random.StatefulSplitGen g => SplitGen (STGen g)Defined in random-1.3.1 · System.Random.StatefulSplitGen g => SplitGen (TGen g)Defined in random-1.3.1 · System.Random.Stateful
The standard pseudo-random number generator.
Instances8Eq, Show, NFData, RandomGen, SplitGen, SeedGen, …
Eq StdGenDefined in random-1.3.1 · System.Random.InternalShow StdGenDefined in random-1.3.1 · System.Random.InternalNFData StdGenDefined in random-1.3.1 · System.Random.InternalRandomGen StdGenDefined in random-1.3.1 · System.Random.InternalSplitGen StdGenDefined in random-1.3.1 · System.Random.InternalSeedGen StdGenDefined in random-1.3.1 · System.Random.SeedMonadSplit StdGen IODefined in MonadRandom-0.6.2.1 · Control.Monad.Random.Classtype SeedSize StdGen = SeedSize SMGenDefined in random-1.3.1 · System.Random.Seed
The class of types for which a uniformly distributed value can be drawn from all possible values of the type.
Instances42Uniform, …
Uniform CBoolDefined in random-1.3.1 · System.Random.InternalUniform CCharDefined in random-1.3.1 · System.Random.InternalUniform CIntDefined in random-1.3.1 · System.Random.InternalUniform CIntMaxDefined in random-1.3.1 · System.Random.InternalUniform CIntPtrDefined in random-1.3.1 · System.Random.InternalUniform CLLongDefined in random-1.3.1 · System.Random.InternalUniform CLongDefined in random-1.3.1 · System.Random.InternalUniform CPtrdiffDefined in random-1.3.1 · System.Random.InternalUniform CSCharDefined in random-1.3.1 · System.Random.InternalUniform CShortDefined in random-1.3.1 · System.Random.InternalUniform CSigAtomicDefined in random-1.3.1 · System.Random.InternalUniform CSizeDefined in random-1.3.1 · System.Random.InternalUniform CUCharDefined in random-1.3.1 · System.Random.InternalUniform CUIntDefined in random-1.3.1 · System.Random.InternalUniform CUIntMaxDefined in random-1.3.1 · System.Random.InternalUniform CUIntPtrDefined in random-1.3.1 · System.Random.InternalUniform CULLongDefined in random-1.3.1 · System.Random.InternalUniform CULongDefined in random-1.3.1 · System.Random.InternalUniform CUShortDefined in random-1.3.1 · System.Random.InternalUniform CWcharDefined in random-1.3.1 · System.Random.InternalUniform Int16Defined in random-1.3.1 · System.Random.InternalUniform Int32Defined in random-1.3.1 · System.Random.InternalUniform Int64Defined in random-1.3.1 · System.Random.InternalUniform Int8Defined in random-1.3.1 · System.Random.InternalUniform BoolDefined in random-1.3.1 · System.Random.InternalUniform CharDefined in random-1.3.1 · System.Random.InternalUniform IntDefined in random-1.3.1 · System.Random.InternalUniform WordDefined in random-1.3.1 · System.Random.InternalUniform Word16Defined in random-1.3.1 · System.Random.InternalUniform Word32Defined in random-1.3.1 · System.Random.InternalUniform Word64Defined in random-1.3.1 · System.Random.InternalUniform Word8Defined in random-1.3.1 · System.Random.InternalUniform ()Defined in random-1.3.1 · System.Random.InternalSeedGen g => Uniform (Seed g)Defined in random-1.3.1 · System.Random.Seed · orphan(Finite a, Uniform a) => Uniform (Maybe a)Defined in random-1.3.1 · System.Random.Internal(Finite a, Uniform a, Finite b, Uniform b) => Uniform (Either a b)Defined in random-1.3.1 · System.Random.Internal(Uniform a, Uniform b) => Uniform (a, b)Defined in random-1.3.1 · System.Random.Internal(Uniform a, Uniform b, Uniform c) => Uniform (a, b, c)Defined in random-1.3.1 · System.Random.Internal(Uniform a, Uniform b, Uniform c, Uniform d) => Uniform (a, b, c, d)Defined in random-1.3.1 · System.Random.Internal(Uniform a, Uniform b, Uniform c, Uniform d, Uniform e) => Uniform (a, b, c, d, e)Defined in random-1.3.1 · System.Random.Internal(Uniform a, Uniform b, Uniform c, Uniform d, Uniform e, Uniform f) => Uniform (a, b, c, d, e, f)Defined in random-1.3.1 · System.Random.Internal(Uniform a, Uniform b, Uniform c, Uniform d, Uniform e, Uniform f, Uniform g) => Uniform (a, b, c, d, e, f, g)Defined in random-1.3.1 · System.Random.Internal
The class of types for which a uniformly distributed value can be drawn from a range.
Instances45UniformRange, …
UniformRange IntegerDefined in random-1.3.1 · System.Random.InternalUniformRange NaturalDefined in random-1.3.1 · System.Random.InternalUniformRange CBoolDefined in random-1.3.1 · System.Random.InternalUniformRange CCharDefined in random-1.3.1 · System.Random.InternalUniformRange CDoubleDefined in random-1.3.1 · System.Random.InternalUniformRange CFloatDefined in random-1.3.1 · System.Random.InternalUniformRange CIntDefined in random-1.3.1 · System.Random.InternalUniformRange CIntMaxDefined in random-1.3.1 · System.Random.InternalUniformRange CIntPtrDefined in random-1.3.1 · System.Random.InternalUniformRange CLLongDefined in random-1.3.1 · System.Random.InternalUniformRange CLongDefined in random-1.3.1 · System.Random.InternalUniformRange CPtrdiffDefined in random-1.3.1 · System.Random.InternalUniformRange CSCharDefined in random-1.3.1 · System.Random.InternalUniformRange CShortDefined in random-1.3.1 · System.Random.InternalUniformRange CSigAtomicDefined in random-1.3.1 · System.Random.InternalUniformRange CSizeDefined in random-1.3.1 · System.Random.InternalUniformRange CUCharDefined in random-1.3.1 · System.Random.InternalUniformRange CUIntDefined in random-1.3.1 · System.Random.InternalUniformRange CUIntMaxDefined in random-1.3.1 · System.Random.InternalUniformRange CUIntPtrDefined in random-1.3.1 · System.Random.InternalUniformRange CULLongDefined in random-1.3.1 · System.Random.InternalUniformRange CULongDefined in random-1.3.1 · System.Random.InternalUniformRange CUShortDefined in random-1.3.1 · System.Random.InternalUniformRange CWcharDefined in random-1.3.1 · System.Random.InternalUniformRange Int16Defined in random-1.3.1 · System.Random.InternalUniformRange Int32Defined in random-1.3.1 · System.Random.InternalUniformRange Int64Defined in random-1.3.1 · System.Random.InternalUniformRange Int8Defined in random-1.3.1 · System.Random.InternalUniformRange BoolDefined in random-1.3.1 · System.Random.InternalUniformRange CharDefined in random-1.3.1 · System.Random.InternalUniformRange DoubleDefined in random-1.3.1 · System.Random.InternalUniformRange FloatDefined in random-1.3.1 · System.Random.InternalUniformRange IntDefined in random-1.3.1 · System.Random.InternalUniformRange WordDefined in random-1.3.1 · System.Random.InternalUniformRange Word16Defined in random-1.3.1 · System.Random.InternalUniformRange Word32Defined in random-1.3.1 · System.Random.InternalUniformRange Word64Defined in random-1.3.1 · System.Random.InternalUniformRange Word8Defined in random-1.3.1 · System.Random.InternalUniformRange ()Defined in random-1.3.1 · System.Random.Internal(UniformRange a, UniformRange b) => UniformRange (a, b)Defined in random-1.3.1 · System.Random.Internal(UniformRange a, UniformRange b, UniformRange c) => UniformRange (a, b, c)Defined in random-1.3.1 · System.Random.Internal(UniformRange a, UniformRange b, UniformRange c, UniformRange d) => UniformRange (a, b, c, d)Defined in random-1.3.1 · System.Random.Internal(UniformRange a, UniformRange b, UniformRange c, UniformRange d, UniformRange e) => UniformRange (a, b, c, d, e)Defined in random-1.3.1 · System.Random.Internal(UniformRange a, UniformRange b, UniformRange c, UniformRange d, UniformRange e, UniformRange f) => UniformRange (a, b, c, d, e, f)Defined in random-1.3.1 · System.Random.Internal(UniformRange a, UniformRange b, UniformRange c, UniformRange d, UniformRange e, UniformRange f, UniformRange g) => UniformRange (a, b, c, d, e, f, g)Defined in random-1.3.1 · System.Random.Internal
Interface for converting a pure pseudo-random number generator to and from non-empty sequence of bytes. Seeds are stored in Little-Endian order regardless of the platform it is being used on, which provides cross-platform compatibility, while providing optimal performance for the most common platform type.
Conversion to and from a Seed serves as a building block for implementing serialization for any pure or frozen pseudo-random number generator.
It is not trivial to implement platform independence. For this reason this type class has two alternative ways of creating an instance for this class. The easiest way for constructing a platform indepent seed is by converting the inner state of a generator to and from a list of 64 bit words using toSeed64 and fromSeed64 respectively. In that case cross-platform support will be handled automaticaly.
:set -XDataKinds -XTypeFamiliesimport Data.Word (Word8, Word32)import Data.Bits ((.|.), shiftR, shiftL)import Data.List.NonEmpty (NonEmpty ((:|)))data FiveByteGen = FiveByteGen Word8 Word32 deriving Show:{instance SeedGen FiveByteGen where type SeedSize FiveByteGen = 5 fromSeed64 (w64 :| _) = FiveByteGen (fromIntegral (w64 `shiftR` 32)) (fromIntegral w64) toSeed64 (FiveByteGen x1 x4) = let w64 = (fromIntegral x1 `shiftL` 32) .|. fromIntegral x4 in (w64 :| []):}
FiveByteGen 0x80 0x01020304FiveByteGen 128 16909060fromSeed (toSeed (FiveByteGen 0x80 0x01020304))FiveByteGen 128 16909060toSeed (FiveByteGen 0x80 0x01020304)Seed [0x04, 0x03, 0x02, 0x01, 0x80]toSeed64 (FiveByteGen 0x80 0x01020304)549772722948 :| []
However, when performance is of utmost importance or default handling of cross platform independence is not sufficient, then an adventurous developer can try implementing conversion into bytes directly with toSeed and fromSeed.
Properties that must hold:
> fromSeed (toSeed gen) == gen
> fromSeed64 (toSeed64 gen) == gen
Note, that there is no requirement for every Seed to roundtrip, eg. this proprty does not even hold for StdGen:
let seed = nonEmptyToSeed (0xab :| [0xff00]) :: Seed StdGenseed == toSeed (fromSeed seed)False
Associated types
Methods
fromSeed :: Seed g -> gConvert from a binary representation to a pseudo-random number generator
toSeed :: g -> Seed gConvert to a binary representation of a pseudo-random number generator
fromSeed64 :: NonEmpty Word64 -> gConstruct pseudo-random number generator from a list of words. Whenever list does not have enough bytes to satisfy the SeedSize requirement, it will be padded with zeros. On the other hand when it has more than necessary, extra bytes will be dropped.
For example if SeedSize is set to 2, then only the lower 16 bits of the first element in the list will be used.
toSeed64 :: g -> NonEmpty Word64Convert pseudo-random number generator to a list of words
In case when SeedSize is not a multiple of 8, then the upper bits of the last word in the list will be set to zero.
Instances8SeedGen, …
SeedGen StdGenDefined in random-1.3.1 · System.Random.SeedSeedGen SMGenDefined in random-1.3.1 · System.Random.SeedSeedGen SMGenDefined in random-1.3.1 · System.Random.SeedSeedGen g => SeedGen (StateGen g)Defined in random-1.3.1 · System.Random.SeedSeedGen g => SeedGen (AtomicGen g)Defined in random-1.3.1 · System.Random.StatefulSeedGen g => SeedGen (IOGen g)Defined in random-1.3.1 · System.Random.StatefulSeedGen g => SeedGen (STGen g)Defined in random-1.3.1 · System.Random.StatefulSeedGen g => SeedGen (TGen g)Defined in random-1.3.1 · System.Random.Stateful
Number of bytes that is required for storing the full state of a pseudo-random number generator. It should be big enough to satisfy the roundtrip property:
> fromSeed (toSeed gen) == gen
Instances8SeedSize, …
type SeedSize StdGen = SeedSize SMGenDefined in random-1.3.1 · System.Random.Seedtype SeedSize SMGen =Defined in random-1.3.1 · System.Random.Seed16
type SeedSize SMGen =Defined in random-1.3.1 · System.Random.Seed8
type SeedSize (StateGen g) = SeedSize gDefined in random-1.3.1 · System.Random.Seedtype SeedSize (AtomicGen g) = SeedSize gDefined in random-1.3.1 · System.Random.Statefultype SeedSize (IOGen g) = SeedSize gDefined in random-1.3.1 · System.Random.Statefultype SeedSize (STGen g) = SeedSize gDefined in random-1.3.1 · System.Random.Statefultype SeedSize (TGen g) = SeedSize gDefined in random-1.3.1 · System.Random.Stateful
module Control.Monad.Random.Class
Same as >>=, but with the arguments interchanged.
as >>= f == f =<< asThe join function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level.
'join bss' can be understood as the do expression
do bs <- bss
bs
Examples
join [[1, 2, 3], [4, 5, 6], [7, 8, 9]][1,2,3,4,5,6,7,8,9]
join (Just (Just 3))Just 3
A common use of join is to run an IO computation returned from
an GHC.Conc.STM transaction, since GHC.Conc.STM transactions
can't perform IO directly. Recall that
GHC.Internal.Conc.atomically :: STM a -> IO a
is used to run GHC.Conc.STM transactions atomically. So, by
specializing the types of GHC.Internal.Conc.atomically and join to
GHC.Internal.Conc.atomically :: STM (IO b) -> IO (IO b)
join :: IO (IO b) -> IO b
we can compose them as
join . GHC.Internal.Conc.atomically :: STM (IO b) -> IO b
to run an GHC.Conc.STM transaction and the IO action it
returns.
Promote a function to a monad. This is equivalent to fmap but specialised to Monads.
Promote a function to a monad, scanning the monadic arguments from left to right.
Examples
liftM2 (+) [0,1] [0,2][0,2,1,3]
liftM2 (+) (Just 1) NothingNothing
liftM2 (+) (+ 3) (* 2) 518
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Conditional execution of Applicative expressions. For example,
Examples
when debug (putStrLn "Debugging")will output the string Debugging if the Boolean value debug
is True, and otherwise do nothing.
putStr "pi:" >> when False (print 3.14159)pi:
Strict version of <$>.
This generalizes the list-based Data.List.filter function.
runIdentity (filterM (Identity . p) xs) == filter p xsExamples
filterM (\x -> do putStrLn ("Keep: " ++ show x ++ "?") answer <- getLine pure (answer == "y")) [1, 2, 3]Keep: 1?yKeep: 2?nKeep: 3?y[1,3]
filterM (\x -> do putStr (show x) x' <- readLn pure (x == x')) [1, 2, 3]122233[2,3]
The foldM function is analogous to foldl, except that its result is
encapsulated in a monad. Note that foldM works from left-to-right over
the list arguments. This could be an issue where (>>) and the `folded
function' are not commutative.
foldM f a1 [x1, x2, ..., xm]
==
do
a2 <- f a1 x1
a3 <- f a2 x2
...
f am xmIf right-to-left evaluation is required, the input list should be reversed.
Like foldM, but discards the result.
Repeat an action indefinitely.
Examples
A common use of forever is to process input from network sockets,
System.IO.Handles, and channels
(e.g. MVar and
Chan).
For example, here is how we might implement an echo server, using forever both to listen for client connections on a network socket and to echo client input on client connection handles:
echoServer :: Socket -> IO ()
echoServer socket = forever $ do
client <- accept socket
forkFinally (echo client) (\_ -> hClose client)
where
echo :: Handle -> IO ()
echo client = forever $
hGetLine client >>= hPutStrLn client
Note that "forever" isn't necessarily non-terminating.
If the action is in a MonadPlus and short-circuits after some number of iterations.
then forever actually returns mzero, effectively short-circuiting its caller.
Conditional failure of Alternative computations. Defined by
guard True = pure ()
guard False = empty
Examples
Common uses of guard include conditionally signalling an error in an error monad and conditionally rejecting the current choice in an Alternative-based parser.
As an example of signalling an error in the error monad Maybe,
consider a safe division function safeDiv x y that returns
Nothing when the denominator y is zero and Just (x `div`
y) otherwise. For example:
safeDiv 4 0Nothing
safeDiv 4 2Just 2
A definition of safeDiv using guards, but not guard:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y | y /= 0 = Just (x `div` y)
| otherwise = Nothing
A definition of safeDiv using guard and Monad do-notation:
safeDiv :: Int -> Int -> Maybe Int
safeDiv x y = do
guard (y /= 0)
return (x `div` y)
The mapAndUnzipM function maps its first argument over a list, returning the result as a pair of lists. This function is mainly used with complicated data structures or a state monad.
Direct MonadPlus equivalent of Data.List.filter.
Examples
The Data.List.filter function is just mfilter specialized to
the list monad:
Data.List.filter = ( mfilter :: (a -> Bool) -> [a] -> [a] )
An example using mfilter with the Maybe monad:
mfilter odd (Just 1)Just 1mfilter odd (Just 2)Nothing
replicateM n act performs the action act n times,
and then returns the list of results.
replicateM n (pure x) == replicate n xExamples
replicateM 3 getLinehiheyahiya["hi","heya","hiya"]
import Control.Monad.StaterunState (replicateM 3 $ state $ \s -> (s, s + 1)) 1([1,2,3],4)
The reverse of when.
Examples
do x <- getLine unless (x == "hi") (putStrLn "hi!")comingupwithexamplesisdifficulthi!
unless (pi > exp 1) NothingJust ()
Evaluate each monadic action in the structure from left to right,
and ignore the results. For a version that doesn't ignore the
results see Data.Traversable.sequence.
sequence_ is just like sequenceA_, but specialised to monadic actions.
void value discards or ignores the result of evaluation, such
as the return value of an System.IO.IO action.
Examples
Replace the contents of a Maybe Int with unit:
void NothingNothing
void (Just 3)Just ()
Replace the contents of an Either Int Int
with unit, resulting in an Either Int :()
void (Left 8675309)Left 8675309
void (Right 8675309)Right ()
Replace every element of a list with unit:
void [1,2,3][(),(),()]
Replace the second element of a pair with unit:
void (1,2)(1,())
Discard the result of an System.IO.IO action:
mapM print [1,2]12[(),()]
void $ mapM print [1,2]12
A type f is a Functor if it provides a function fmap which, given any types a and b
lets you apply any function from (a -> b) to turn an f a into an f b, preserving the
structure of f. Furthermore f needs to adhere to the following:
Note, that the second law follows from the free theorem of the type fmap and the first law, so you need only check that the former condition holds. See these articles by School of Haskell or David Luposchainsky for an explanation.
Methods
fmap :: (a -> b) -> f a -> f bfmap is used to apply a function of type
(a -> b)to a value of typef a, where f is a functor, to produce a value of typef b. Note that for any type constructor with more than one parameter (e.g.,Either), only the last type parameter can be modified with fmap (e.g.,binEither a b).Some type constructors with two parameters or more have a
instance that allows both the last and the penultimate parameters to be mapped over.Data.BifunctorExamples
Convert from a
Maybe Intto aMaybe Stringusing show:Example2 expressions fmap show NothingNothingfmap show (Just 3)Just "3"
Convert from an
Either Int Intto anEither Int Stringusing show:Example2 expressions fmap show (Left 17)Left 17fmap show (Right 17)Right "17"
Double each element of a list:
Example1 expression fmap (*2) [1,2,3][2,4,6]
Apply even to the second element of a pair:
Example1 expression fmap even (2,2)(2,True)
It may seem surprising that the function is only applied to the last element of the tuple compared to the list example above which applies it to every element in the list. To understand, remember that tuples are type constructors with multiple type parameters: a tuple of 3 elements
(a,b,c)can also be written(,,) a b cand itsFunctorinstance is defined forFunctor ((,,) a b)(i.e., only the third parameter is free to be mapped over withfmap).It explains why
fmapcan be used with tuples containing values of different types as in the following example:Example1 expression fmap even ("hello", 1.0, 4)("hello",1.0,True)
(<$) :: a -> f b -> f ainfixl 4Replace all locations in the input with the same value. The default definition is
fmap . const, but this may be overridden with a more efficient version.Examples
Perform a computation with Maybe and replace the result with a constant value if it is Just:
Example2 expressions 'a' <$ Just 2Just 'a''a' <$ NothingNothing
Instances105Functor, …
Functor ComplexDefined in base-4.22.0.0 · Data.ComplexFunctor FirstDefined in base-4.22.0.0 · Data.SemigroupFunctor LastDefined in base-4.22.0.0 · Data.SemigroupFunctor MaxDefined in base-4.22.0.0 · Data.SemigroupFunctor MinDefined in base-4.22.0.0 · Data.SemigroupFunctor ArgDescrDefined in base-4.22.0.0 · System.Console.GetOptFunctor ArgOrderDefined in base-4.22.0.0 · System.Console.GetOptFunctor OptDescrDefined in base-4.22.0.0 · System.Console.GetOptFunctor PutDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.InternalFunctor PprMDefined in ghc-boot-th-9.14.1 · GHC.Boot.TH.PprLibFunctor NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanFunctor STMDefined in ghc-internal-9.1401.0 · GHC.Internal.Conc.SyncFunctor HandlerDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.ExceptionFunctor IdentityDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.IdentityFunctor FirstDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidFunctor LastDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidFunctor DownDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.OrdFunctor DualDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor ProductDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor SumDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor ZipListDefined in ghc-internal-9.1401.0 · GHC.Internal.Functor.ZipListFunctor NoIODefined in ghc-internal-9.1401.0 · GHC.Internal.GHCiFunctor Par1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor GenClosureDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor GenStackFieldDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor GenStackFrameDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor GenStgStackClosureDefined in ghc-internal-9.1401.0 · GHC.Internal.Heap.ClosuresFunctor MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxFunctor TyVarBndrDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxFunctor PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPFunctor ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPFunctor ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecFunctor SoloDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor IODefined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor AnnotDetailsDefined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJFunctor DocDefined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJFunctor SpanDefined in pretty-1.1.3.6 · Text.PrettyPrint.Annotated.HughesPJFunctor ArrayDefined in primitive-0.9.1.0 · Data.Primitive.ArrayFunctor SmallArrayDefined in primitive-0.9.1.0 · Data.Primitive.SmallArrayFunctor []Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.ProxyFunctor U1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor V1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Arg a)Defined in base-4.22.0.0 · Data.SemigroupFunctor (Array i)Defined in ghc-internal-9.1401.0 · GHC.Internal.ArrFunctor (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpFunctor (Either a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.EitherFunctor (StateL s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsFunctor (StateR s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsFunctor (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.STFunctor (Tuple2 a)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor f => Functor (Lift f)Defined in transformers-0.6.1.2 · Control.Applicative.LiftFunctor m => Functor (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonad m => Functor (WrappedMonad m)Defined in base-4.22.0.0 · Control.ApplicativeArrow a => Functor (ArrowMonad a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowFunctor (Const m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.ConstFunctor (URec Char)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Double)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Float)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Int)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec Word)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (URec (Ptr ()))Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple3 a b)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor (Constant a)Defined in transformers-0.6.1.2 · Data.Functor.ConstantFunctor (t m) => Functor (LiftingAccum t m)Defined in mtl-2.3.1 · Control.Monad.AccumFunctor (t m) => Functor (LiftingSelect t m)Defined in mtl-2.3.1 · Control.Monad.SelectFunctor f => Functor (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidFunctor f => Functor (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalFunctor f => Functor (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor f => Functor (Backwards f)Defined in transformers-0.6.1.2 · Control.Applicative.BackwardsDerived instance.
Functor f => Functor (Reverse f)Defined in transformers-0.6.1.2 · Data.Functor.ReverseDerived instance.
Functor m => Functor (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyFunctor m => Functor (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictFunctor m => Functor (Kleisli m a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowFunctor m => Functor (AccumT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.AccumFunctor m => Functor (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptFunctor m => Functor (IdentityT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityFunctor m => Functor (ReaderT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderFunctor m => Functor (SelectT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.SelectFunctor m => Functor (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.LazyFunctor m => Functor (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.StrictFunctor m => Functor (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPSFunctor m => Functor (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.LazyFunctor m => Functor (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.StrictMonad m => Functor (StateT s m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsArrow a => Functor (WrappedArrow a b)Defined in base-4.22.0.0 · Control.Applicative(Generic1 f, Functor (Rep1 f)) => Functor (Generically1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (K1 i c)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple4 a b c)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor (ContT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ContFunctor ((->) r)Defined in ghc-internal-9.1401.0 · GHC.Internal.Base(Functor f, Functor g) => Functor (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(Functor f, Functor g) => Functor (Sum f g)Defined in base-4.22.0.0 · Data.Functor.Sum(Functor f, Functor g) => Functor (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Functor f, Functor g) => Functor (f :+: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple5 a b c d)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor f => Functor (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor m => Functor (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPSFunctor m => Functor (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.LazyFunctor m => Functor (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Strict(Functor f, Functor g) => Functor (Compose f g)Defined in base-4.22.0.0 · Data.Functor.Compose(Functor f, Functor g) => Functor (f :.: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsFunctor (Tuple6 a b c d e)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseFunctor (Tuple7 a b c d e f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Base
The Monad class defines the basic operations over a monad,
a concept from a branch of mathematics known as category theory.
From the perspective of a Haskell programmer, however, it is best to
think of a monad as an abstract datatype of actions.
Haskell's do expressions provide a convenient syntax for writing
monadic expressions.
Instances of Monad should satisfy the following:
- Left identity
- Right identity
- Associativity
Furthermore, the Monad and Applicative operations should relate as follows:
The above laws imply:
and that pure and (<*>) satisfy the applicative functor laws.
The instances of Monad for GHC.List.List, Maybe and System.IO.IO
defined in the Prelude satisfy these laws.
Methods
(>>=) :: m a -> (a -> m b) -> m binfixl 1Sequentially compose two actions, passing any value produced by the first as an argument to the second.
'
as >>= bs' can be understood as thedoexpressiondo a <- as bs aAn alternative name for this function is 'bind', but some people may refer to it as 'flatMap', which results from it being equivalent to
\x f -> join (fmap f x) :: Monad m => m a -> (a -> m b) -> m bwhich can be seen as mapping a value with
Monad m => m a -> m (m b)and then 'flattening'm (m b)tom busing join.(>>) :: m a -> m b -> m binfixl 1Sequentially compose two actions, discarding any value produced by the first, like sequencing operators (such as the semicolon) in imperative languages.
'
as >> bs' can be understood as thedoexpressiondo as bsor in terms of
as(>>=)as >>= const bsreturn :: a -> m aInject a value into the monadic type. This function should not be different from its default implementation as pure. The justification for the existence of this function is merely historic.
Instances67Monad, …
Monad ComplexDefined in base-4.22.0.0 · Data.ComplexMonad FirstDefined in base-4.22.0.0 · Data.SemigroupMonad LastDefined in base-4.22.0.0 · Data.SemigroupMonad MaxDefined in base-4.22.0.0 · Data.SemigroupMonad MinDefined in base-4.22.0.0 · Data.SemigroupMonad PutDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.InternalMonad PprMDefined in ghc-boot-th-9.14.1 · GHC.Boot.TH.PprLibMonad NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.NonEmpty · orphanMonad STMDefined in ghc-internal-9.1401.0 · GHC.Internal.Conc.SyncMonad IdentityDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.IdentityMonad FirstDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonad LastDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonad DownDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.OrdMonad DualDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad ProductDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad SumDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad NoIODefined in ghc-internal-9.1401.0 · GHC.Internal.GHCiMonad Par1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonad MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxMonad PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonad ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonad ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecMonad SoloDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad IODefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad ArrayDefined in primitive-0.9.1.0 · Data.Primitive.ArrayMonad SmallArrayDefined in primitive-0.9.1.0 · Data.Primitive.SmallArrayMonad []Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.ProxyMonad U1Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonad (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpMonad (Either e)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.EitherMonad (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.STMonad m => Monad (WrappedMonad m)Defined in base-4.22.0.0 · Control.ApplicativeMonad m => Monad (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonoid a => Monad (Tuple2 a)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseArrowApply a => Monad (ArrowMonad a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonad (t m) => Monad (LiftingAccum t m)Defined in mtl-2.3.1 · Control.Monad.AccumMonad (t m) => Monad (LiftingSelect t m)Defined in mtl-2.3.1 · Control.Monad.SelectMonad f => Monad (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonad f => Monad (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonad f => Monad (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonad m => Monad (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonad m => Monad (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonad m => Monad (Kleisli m a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonad m => Monad (StateT s m)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.UtilsMonad m => Monad (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonad m => Monad (IdentityT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityMonad m => Monad (ReaderT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderMonad m => Monad (SelectT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.SelectMonad m => Monad (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.LazyMonad m => Monad (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.StrictMonad m => Monad (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPSMonad m => Monad (Reverse m)Defined in transformers-0.6.1.2 · Data.Functor.ReverseDerived instance.
(Monoid a, Monoid b) => Monad (Tuple3 a b)Defined in ghc-internal-9.1401.0 · GHC.Internal.Base(Monoid w, Functor m, Monad m) => Monad (AccumT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Accum(Monoid w, Monad m) => Monad (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Lazy(Monoid w, Monad m) => Monad (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.StrictMonad (ContT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ContMonad ((->) r)Defined in ghc-internal-9.1401.0 · GHC.Internal.Base(Monad f, Monad g) => Monad (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(Monad f, Monad g) => Monad (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Monoid a, Monoid b, Monoid c) => Monad (Tuple4 a b c)Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonad f => Monad (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonad m => Monad (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPS(Monoid w, Monad m) => Monad (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Lazy(Monoid w, Monad m) => Monad (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Strict
Monads that also support choice and failure.
Instances36MonadPlus, …
MonadPlus STMDefined in ghc-internal-9.1401.0 · GHC.Internal.Conc.SyncMonadPlus MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonadPlus PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadPlus ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadPlus ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecMonadPlus IODefined in ghc-internal-9.1401.0 · GHC.Internal.BaseMonadPlus ArrayDefined in primitive-0.9.1.0 · Data.Primitive.ArrayMonadPlus SmallArrayDefined in primitive-0.9.1.0 · Data.Primitive.SmallArrayMonadPlus []Defined in ghc-internal-9.1401.0 · GHC.Internal.BaseCombines lists by concatenation, starting from the empty list.
Monad m => MonadPlus (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadPlus ProxyDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.ProxyMonadPlus U1Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(ArrowApply a, ArrowPlus a) => MonadPlus (ArrowMonad a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonadPlus f => MonadPlus (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonadPlus f => MonadPlus (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.Semigroup.InternalMonadPlus f => MonadPlus (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonadPlus m => MonadPlus (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadPlus m => MonadPlus (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadPlus m => MonadPlus (Kleisli m a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.ArrowMonadPlus m => MonadPlus (IdentityT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityMonadPlus m => MonadPlus (ReaderT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderMonadPlus m => MonadPlus (SelectT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.SelectMonadPlus m => MonadPlus (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.LazyMonadPlus m => MonadPlus (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.StrictMonadPlus m => MonadPlus (Reverse m)Defined in transformers-0.6.1.2 · Data.Functor.ReverseDerived instance.
(Functor m, MonadPlus m) => MonadPlus (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPS(Monad m, Monoid e) => MonadPlus (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Except(Monoid w, Functor m, MonadPlus m) => MonadPlus (AccumT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Accum(Monoid w, MonadPlus m) => MonadPlus (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Lazy(Monoid w, MonadPlus m) => MonadPlus (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Strict(MonadPlus f, MonadPlus g) => MonadPlus (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(MonadPlus f, MonadPlus g) => MonadPlus (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.GenericsMonadPlus f => MonadPlus (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Generics(Functor m, MonadPlus m) => MonadPlus (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPS(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Lazy(Monoid w, MonadPlus m) => MonadPlus (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Strict
When a value is bound in do-notation, the pattern on the left
hand side of <- might not match. In this case, this class
provides a function to recover.
A Monad without a MonadFail instance may only be used in conjunction
with pattern that always match, such as newtypes, tuples, data types with
only a single data constructor, and irrefutable patterns (~pat).
Instances of MonadFail should satisfy the following law: fail s should
be a left zero for >>=,
fail s >>= f = fail s
If your Monad is also MonadPlus, a popular definition is
fail _ = mzero
fail s should be an action that runs in the monad itself, not an
exception (except in instances of MonadIO). In particular,
fail should not be implemented in terms of error.
Methods
fail :: HasCallStack => String -> m a
Instances28MonadFail, …
MonadFail MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FailMonadFail QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxMonadFail PDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadFail ReadPDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPMonadFail ReadPrecDefined in ghc-internal-9.1401.0 · GHC.Internal.Text.ParserCombinators.ReadPrecMonadFail IODefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FailMonadFail ArrayDefined in primitive-0.9.1.0 · Data.Primitive.ArrayMonadFail SmallArrayDefined in primitive-0.9.1.0 · Data.Primitive.SmallArrayMonadFail []Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FailMonad m => MonadFail (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadFail f => MonadFail (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Data.MonoidMonadFail m => MonadFail (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadFail m => MonadFail (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadFail m => MonadFail (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadFail m => MonadFail (IdentityT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityMonadFail m => MonadFail (ReaderT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderMonadFail m => MonadFail (SelectT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.SelectMonadFail m => MonadFail (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.LazyMonadFail m => MonadFail (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.StrictMonadFail m => MonadFail (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPSMonadFail m => MonadFail (Reverse m)Defined in transformers-0.6.1.2 · Data.Functor.Reverse(Monoid w, MonadFail m) => MonadFail (AccumT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Accum(Monoid w, MonadFail m) => MonadFail (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Lazy(Monoid w, MonadFail m) => MonadFail (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.StrictMonadFail m => MonadFail (ContT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ContMonadFail m => MonadFail (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPS(Monoid w, MonadFail m) => MonadFail (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Lazy(Monoid w, MonadFail m) => MonadFail (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Strict
Map each element of a structure to a monadic action, evaluate
these actions from left to right, and collect the results. For
a version that ignores the results see Data.Foldable.mapM_.
Examples
mapM is literally a traverse with a type signature restricted to Monad. Its implementation may be more efficient due to additional power of Monad.
Evaluate each monadic action in the structure from left to
right, and collect the results. For a version that ignores the
results see Data.Foldable.sequence_.
Examples
Basic usage:
The first two examples are instances where the input and and output of sequence are isomorphic.
sequence $ Right [1,2,3,4][Right 1,Right 2,Right 3,Right 4]
sequence $ [Right 1,Right 2,Right 3,Right 4]Right [1,2,3,4]
The following examples demonstrate short circuit behavior for sequence.
sequence $ Left [1,2,3,4]Left [1,2,3,4]
sequence $ [Left 0, Right 1,Right 2,Right 3,Right 4]Left 0
fix f is the least fixed point of the function f,
i.e. the least defined x such that f x = x.
When f is strict, this means that because, by the definition of strictness,
f ⊥ = ⊥ and such the least defined fixed point of any strict function is ⊥.
Examples
We can write the factorial function using direct recursion as
let fac n = if n <= 1 then 1 else n * fac (n-1) in fac 5120
This uses the fact that Haskell’s let introduces recursive bindings. We can
rewrite this definition using fix,
Instead of making a recursive call, we introduce a dummy parameter rec;
when used within fix, this parameter then refers to fix’s argument, hence
the recursion is reintroduced.
fix (\rec n -> if n <= 1 then 1 else n * rec (n-1)) 5120
Using fix, we can implement versions of repeat as fix .
and cycle as (:)fix . (++)
take 10 $ fix (0:)[0,0,0,0,0,0,0,0,0,0]
map (fix (\rec n -> if n < 2 then n else rec (n - 1) + rec (n - 2))) [1..10][1,1,2,3,5,8,13,21,34,55]
Implementation Details
The current implementation of fix uses structural sharing
fix f = let x = f x in xA more straightforward but non-sharing version would look like
fix f = f (fix f)Monads having fixed points with a 'knot-tying' semantics. Instances of MonadFix should satisfy the following laws:
- Purity
- Left shrinking (or Tightening)
mfix (\x -> a >>= \y -> f x y) = a >>= \y -> mfix (\x -> f x y)- Sliding
mfix (liftM h . f) = liftM h (mfix (f . h)), for strict
h.
- Nesting
This class is used in the translation of the recursive do notation
supported by GHC and Hugs.
Instances47MonadFix, …
MonadFix ComplexDefined in base-4.22.0.0 · Data.ComplexMonadFix FirstDefined in base-4.22.0.0 · Data.SemigroupMonadFix LastDefined in base-4.22.0.0 · Data.SemigroupMonadFix MaxDefined in base-4.22.0.0 · Data.SemigroupMonadFix MinDefined in base-4.22.0.0 · Data.SemigroupMonadFix NonEmptyDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix IdentityDefined in ghc-internal-9.1401.0 · GHC.Internal.Data.Functor.IdentityMonadFix FirstDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix LastDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix DownDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix DualDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix ProductDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix SumDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix Par1Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix MaybeDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxIf the function passed to mfix inspects its argument, the resulting action will throw a FixIOException.
MonadFix SoloDefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix IODefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix ArrayDefined in primitive-0.9.1.0 · Data.Primitive.ArrayMonadFix SmallArrayDefined in primitive-0.9.1.0 · Data.Primitive.SmallArrayMonadFix []Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonoid a => MonadFix (Tuple2 a)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.ST.Lazy.ImpMonadFix (Either e)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix (ST s)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix m => MonadFix (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadFix f => MonadFix (Ap f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix f => MonadFix (Alt f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix f => MonadFix (Rec1 f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix m => MonadFix (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadFix m => MonadFix (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadFix m => MonadFix (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadFix m => MonadFix (IdentityT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityMonadFix m => MonadFix (ReaderT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderMonadFix m => MonadFix (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.LazyMonadFix m => MonadFix (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.StrictMonadFix m => MonadFix (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPS(Monoid w, Functor m, MonadFix m) => MonadFix (AccumT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Accum(Monoid w, MonadFix m) => MonadFix (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Lazy(Monoid w, MonadFix m) => MonadFix (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.StrictMonadFix ((->) r)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.Fix(MonadFix f, MonadFix g) => MonadFix (Product f g)Defined in base-4.22.0.0 · Data.Functor.Product(MonadFix f, MonadFix g) => MonadFix (f :*: g)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix f => MonadFix (M1 i c f)Defined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.FixMonadFix m => MonadFix (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPS(Monoid w, MonadFix m) => MonadFix (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Lazy(Monoid w, MonadFix m) => MonadFix (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Strict
Monads in which IO computations may be embedded. Any monad built by applying a sequence of monad transformers to the IO monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO is a transformer of monads:
Methods
liftIO :: IO a -> m aLift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).
Example
import Control.Monad.Trans.State -- from the "transformers" library printState :: Show s => StateT s IO () printState = do state <- get liftIO $ print stateHad we omitted
liftIO, we would have ended up with this error:• Couldn't match type ‘IO’ with ‘StateT s IO’ Expected type: StateT s IO () Actual type: IO ()The important part here is the mismatch between
StateT s IO ()andIO ().Luckily, we know of a function that takes an
IO aand returns an(m a):liftIO, enabling us to run the program and see the expected results:> evalStateT printState "hello" "hello" > evalStateT printState 3 3
Instances19MonadIO, …
MonadIO QDefined in ghc-internal-9.1401.0 · GHC.Internal.TH.SyntaxMonadIO IODefined in ghc-internal-9.1401.0 · GHC.Internal.Control.Monad.IO.ClassMonadIO m => MonadIO (MaybeT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonadIO m => MonadIO (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadIO m => MonadIO (RandT g m)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadIO m => MonadIO (ExceptT e m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadIO m => MonadIO (IdentityT m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityMonadIO m => MonadIO (ReaderT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderMonadIO m => MonadIO (SelectT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.SelectMonadIO m => MonadIO (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.LazyMonadIO m => MonadIO (StateT s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.StrictMonadIO m => MonadIO (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPS(Monoid w, Functor m, MonadIO m) => MonadIO (AccumT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Accum(Monoid w, MonadIO m) => MonadIO (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.Lazy(Monoid w, MonadIO m) => MonadIO (WriterT w m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.StrictMonadIO m => MonadIO (ContT r m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ContMonadIO m => MonadIO (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPS(Monoid w, MonadIO m) => MonadIO (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Lazy(Monoid w, MonadIO m) => MonadIO (RWST r w s m)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.Strict
class (forall (m :: Type -> Type). Monad m => Monad (t m)) => MonadTrans (t :: (Type -> Type) -> Type -> Type) whereThe class of monad transformers.
For any monad m, the result t m should also be a monad,
and lift should be a monad transformation from m to t m,
i.e. it should satisfy the following laws:
Since 0.6.0.0 and for GHC 8.6 and later, the requirement that t m
be a Monad is enforced by the implication constraint
forall m. Monad m => Monad (t m) enabled by the
QuantifiedConstraints extension.
Ambiguity error with GHC 9.0 to 9.2.2
These versions of GHC have a bug (https://gitlab.haskell.org/ghc/ghc/-/issues/20582) which causes constraints like
(MonadTrans t, forall m. Monad m => Monad (t m)) => ...
to be reported as ambiguous. For transformers 0.6 and later, this can be fixed by removing the second constraint, which is implied by the first.
Instances17MonadTrans, …
MonadTrans MaybeTDefined in transformers-0.6.1.2 · Control.Monad.Trans.MaybeMonoid w => MonadTrans (AccumT w)Defined in transformers-0.6.1.2 · Control.Monad.Trans.AccumMonoid w => MonadTrans (WriterT w)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.LazyMonoid w => MonadTrans (WriterT w)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.StrictMonadTrans IdentityTDefined in transformers-0.6.1.2 · Control.Monad.Trans.IdentityMonadTrans (RandT g)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.LazyMonadTrans (RandT g)Defined in MonadRandom-0.6.2.1 · Control.Monad.Trans.Random.StrictMonadTrans (ExceptT e)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ExceptMonadTrans (ReaderT r)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ReaderMonadTrans (SelectT r)Defined in transformers-0.6.1.2 · Control.Monad.Trans.SelectMonadTrans (StateT s)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.LazyMonadTrans (StateT s)Defined in transformers-0.6.1.2 · Control.Monad.Trans.State.StrictMonadTrans (WriterT w)Defined in transformers-0.6.1.2 · Control.Monad.Trans.Writer.CPSMonadTrans (ContT r)Defined in transformers-0.6.1.2 · Control.Monad.Trans.ContMonoid w => MonadTrans (RWST r w s)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.LazyMonoid w => MonadTrans (RWST r w s)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.StrictMonadTrans (RWST r w s)Defined in transformers-0.6.1.2 · Control.Monad.Trans.RWS.CPS