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 operationsStateT transfers the state of the solution as it progressively evaluatesReaderT has a fixed state Problem space solutionWriterT has a leaderboard constantly updated with the best version (s) so farMaybeTnecessary, 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 . . , , , .
, ? , , , , ? , , , , ?