state-monad

How do I avoid referring to all state variables when updating only a few?

五迷三道 提交于 2019-12-09 02:12:32
问题 An idiom I use for composing a couple of procedures (with memory) is as follows: p1 :: State (Int, String) () p1 = do (a, b) <- get ... do something ... put (a', b) p2 :: State (Int, String) () p2 = do (a, b) <- get ... do something else ... put (a, b') main = do ... initializing a0 b0 ... print . flip evalState (a0, b0) . sequence $ replicate 10 p1 ++ repeat p2 However, as the number of state variable grows, this quickly gets way more verbose than necessary: p1 :: State (Int, String, Bool,

Adjoint functors determine monad transformers, but where's lift?

只愿长相守 提交于 2019-12-07 02:46:54
问题 I'm intrigued by the construction described here for determining a monad transformer from adjoint functors. Here's some code that summarizes the basic idea: {-# LANGUAGE MultiParamTypeClasses #-} import Control.Monad newtype Three g f m a = Three { getThree :: g (m (f a)) } class (Functor f, Functor g) => Adjoint f g where counit :: f (g a) -> a unit :: a -> g (f a) instance (Adjoint f g, Monad m) => Monad (Three g f m) where return = Three . fmap return . unit m >>= f = Three $ fmap (>>=

Stuck in the State Monad

一世执手 提交于 2019-12-06 23:49:33
问题 I want to create a graph structure using an IntMap of nodes and unique keys. This topic has been covered well here and here. I understand how the state monad works by basically wrapping a function of state -> (val,state) in a newtype so we can create a monad instance for it. I have read about the topic quite a bit. I still cant seem to get my head around how I can get unique (or just incremental) values throughout the execution of my program. It's easy enough to get a run of successive IDs,

What is the difference between `ioToST` and `unsafeIOToST` from GHC.IO

落花浮王杯 提交于 2019-12-06 19:24:07
问题 What can the differences and intended uses be for ioToST and unsafeSTToIO defined in GHC.IO? -- --------------------------------------------------------------------------- -- Coercions between IO and ST -- | A monad transformer embedding strict state transformers in the 'IO' -- monad. The 'RealWorld' parameter indicates that the internal state -- used by the 'ST' computation is a special one supplied by the 'IO' -- monad, and thus distinct from those used by invocations of 'runST'. stToIO ::

Adjoint functors determine monad transformers, but where's lift?

走远了吗. 提交于 2019-12-05 06:41:15
I'm intrigued by the construction described here for determining a monad transformer from adjoint functors. Here's some code that summarizes the basic idea: {-# LANGUAGE MultiParamTypeClasses #-} import Control.Monad newtype Three g f m a = Three { getThree :: g (m (f a)) } class (Functor f, Functor g) => Adjoint f g where counit :: f (g a) -> a unit :: a -> g (f a) instance (Adjoint f g, Monad m) => Monad (Three g f m) where return = Three . fmap return . unit m >>= f = Three $ fmap (>>= counit . fmap (getThree . f)) (getThree m) instance (Adjoint f g, Monad m) => Applicative (Three g f m)

What is the difference between `ioToST` and `unsafeIOToST` from GHC.IO

半腔热情 提交于 2019-12-05 00:44:31
What can the differences and intended uses be for ioToST and unsafeSTToIO defined in GHC.IO ? -- --------------------------------------------------------------------------- -- Coercions between IO and ST -- | A monad transformer embedding strict state transformers in the 'IO' -- monad. The 'RealWorld' parameter indicates that the internal state -- used by the 'ST' computation is a special one supplied by the 'IO' -- monad, and thus distinct from those used by invocations of 'runST'. stToIO :: ST RealWorld a -> IO a stToIO (ST m) = IO m ioToST :: IO a -> ST RealWorld a ioToST (IO m) = (ST m) --

Haskell Data Type With References

会有一股神秘感。 提交于 2019-12-04 21:45:18
问题 I'm implementing Ukkonen's algorithm, which requires that all leaves of a tree contain a reference to the same integer, and I'm doing it in Haskell to learn more about the language. However, I'm having a hard time writing out a data type that does this. -- Node has children, indexes of info on the edge -- to it, and an optional suffix link. -- Leaf has a beginning index of the info, but the -- end index is always an incrementing variable index. data STree = Node [STree] (Int, Int) (Maybe

Breadth-First Search using State monad in Haskell

心已入冬 提交于 2019-12-04 10:51:31
问题 Recently, I've asked a question for building DFS tree from Graph in Stackoverflow and had learned that it can be simply implemented by using State Monad. DFS in haskell While DFS requires to track only visited nodes, so that we can use 'Set' or 'List' or some sort of linear data structure to track visited nodes, BFS requires 'visited node' and 'queue' data structure to be accomplished. My pseudocode for BFS is Q = empty queue T = empty Tree mark all nodes except u as unvisited while Q is

Is it possible to implement `(Applicative m) => Applicative (StateT s m)`?

给你一囗甜甜゛ 提交于 2019-12-04 10:10:40
问题 I'm currently working on Data.Fresh and Control.Monad.Trans.Fresh , which resp. define an interface for generating fresh variables, and a monad transformer which implements this interface. I initially thought it would be possible to implement the Applicative instance for my FreshT v m with the only requirement that Applicative m exists. However, I got stuck and it seemed like I need to require Monad m . Not trusting my Haskell-fu, I then turned to the transformers package, and was surprised

Pure functional Random number generator - State monad

倾然丶 夕夏残阳落幕 提交于 2019-12-04 06:52:17
The book ' Functional Programming in Scala ' demonstrates an example of pure functional random number generator as below trait RNG { def nextInt: (Int, RNG) } object RNG { def simple(seed: Long): RNG = new RNG { def nextInt = { val seed2 = (seed*0x5DEECE66DL + 0xBL) & ((1L << 48) - 1) ((seed2 >>> 16).asInstanceOf[Int], simple(seed2)) } } } The usage will look like val (randomNumber,nextState) = rng.nextInt I do get the part that it's a pure function as it returns the next state and leaves it on the API client to use it to call nextInt the next time it would need a random number but what I did