Monad Transformer stacks with MaybeT and RandT

I'm trying to find out how Monad Transformers work by redistributing what I wrote when I first found out Haskell. It has quite a few components that can be replaced with a rather large stack of Monad Transformers.

I started writing a type alias for my stack:

type SolverT a = MaybeT
                    (WriterT Leaderboard
                        (ReaderT Problem
                            (StateT SolutionState
                                (Rand StdGen)))) a

Quick summary:

  • Randflows through StdGenused in various random operations
  • StateT transfers the state of the solution as it progressively evaluates
  • ReaderT has a fixed state Problem space solution
  • WriterT has a leaderboard constantly updated with the best version (s) so far
  • MaybeTnecessary, since both the problem and the state of the solution are used lookupfrom Data.Map, and any error in how they are configured will lead toNothing

a Nothing "" , Map / ( , ). Maybe, fromJust.

, , MaybeT , , Nothing SolverT a , - .

[EDIT: , , / , ]

, RandT . lift RandT. .

MonadReader, , :

instance (MonadReader r m,RandomGen g) => MonadReader r (RandT g m) where
    ask = undefined
    local = undefined
    reader = undefined

lift, liftRand liftRandT . , , ?

1

[EDIT: , , / , ]

, MonadRandom ( MaybeT), :

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

WriterT, ReaderT StateT, MonadRandom. : StateT WriterT , Reader. , :

No instance for (MonadRandom (ReaderT Problem (StateT SolutionState (Rand StdGen))))
  arising from a use of `getRandomR'

, .

2

:

randomCity :: SolverT City
randomCity = do
    cits <- asks getCities
    x <- getRandomR (0,M.size cits -1)
    --rc <- M.lookup x cits
    return undefined --rc

, , , , . , . , . , :

Couldn't match type `Maybe'
              with `MaybeT
                      (WriterT
                         Leaderboard
                         (ReaderT Problem (StateT SolutionState (Rand StdGen))))'
Expected type: MaybeT
                 (WriterT
                    Leaderboard (ReaderT Problem (StateT SolutionState (Rand StdGen))))
                 City
  Actual type: Maybe City

, , . Monads (\s -> (a,s)), Maybe Just a | Nothing. , ask Reader r a, lookup k m Maybe a.

, , GHCI :

> :t ask
ask :: MonadReader r m => m r
> :t (Just 5)
(Just 5) :: Num a => Maybe a
> :t MaybeT 5
MaybeT 5 :: Num (m (Maybe a)) => MaybeT m a

, , . MaybeT , MonadMaybe typeclass.

, lift - MaybeT, MaybeT m a. Maybe a, , do <-.

3

, , , . Solver . . , , , .

, ? , , , , ? , , , , ?

+4
1

1

. RandT.

2

lookup Maybe, MaybeT. MonadMaybe , MonadPlus ( Alternative) - pure/return Just empty/mzero Nothing.

lookupA :: (Alternative f, Ord k) => k -> M.Map k v -> f v
lookupA k = maybe empty pure . M.lookup k

lookupA, ,

, RWST, , , StateT/ReaderT/WriterT.

type Solver a = RWST Problem Leaderboard SolutionState (MaybeT (Rand StdGen)) a

type Solver a = MaybeT (RWST Problem Leaderboard SolutionState (Rand StdGen)) a

, . , Leaderboard, .

3

- . SolutionState.

import Control.Applicative
import Control.Monad.Random
import Control.Monad.Random.Class
import Control.Monad.Trans
import Control.Monad.Trans.Maybe
import Control.Monad.RWS
import qualified Data.Map as M
import Data.Monoid
import System.Random

-- Dummy data types to satisfy the compiler
data Problem = Problem
data Leaderboard = Leaderboard
data SolutionState = SolutionState
data City = City

instance Monoid Leaderboard where
  mempty = Leaderboard
  mappend _ _ = Leaderboard

-- dummy function
getCities :: Problem -> M.Map Int City
getCities _ = M.singleton 0 City

-- the actual sample code

type Solver a = RWST Problem Leaderboard SolutionState (MaybeT (Rand StdGen)) a

lookupA :: (Alternative f, Ord k) => k -> M.Map k v -> f v
lookupA k = maybe empty pure . M.lookup k

randomCity :: Solver City
randomCity = do
    cits <- asks getCities
    x <- getRandomR (0, M.size cits - 1)
    lookupA x cits
+3

Source: https://habr.com/ru/post/1570085/


All Articles