Questions tagged [state-monad]

A monad allowing state information to be attached to calculations

A state monad allows a programmer to attach state information of any type to a calculation. Given any value type, the corresponding type in the state monad is a function which accepts a state, then outputs a new state along with a return value. Wikipedia has a brief overview.

399 questions
6
votes
2 answers

Applicative instance for State and other MTL monads?

Looking at the docs for Control.Applicative, I notice that they have instance declarations for certain monads (e.g. IO, Maybe and notably ST), but there are no instances for MTL monads such as State and RWS. Instead it looks like there's a…
mergeconflict
  • 8,156
  • 34
  • 63
6
votes
1 answer

In composite StateT / Maybe monad, how to take either possibility that succeeds?

In Haskell, here's a monad that combines the State and Maybe monads: type StatefulMaybe a = StateT Int Maybe a This is a computation that can succeed (returning a value) or fail. If it succeeds, it carries a state along with the returned…
Adam Dingle
  • 3,114
  • 1
  • 16
  • 14
6
votes
1 answer

Is there any significant difference between StateT over Reader and ReaderT over State?

When I design my programming model I always have a dilemma which approach is better: type MyMonad1 = StateT MyState (Reader Env) type MyMonad2 = ReaderT Env (State MyState) What are the benefits and trade offs between using one monad stack over…
radrow
  • 6,419
  • 4
  • 26
  • 53
6
votes
1 answer

Interrupting lengthy pure computation in MonadState

I can't grasp the correct way of interrupting lengthy pure computation on SIGINT signal. In the simple example below, I have slowFib function that simulates lengthy computation. When it is run just in IO monad I can terminate it with C-c (using…
6
votes
1 answer

What's the proper way to emulate stateful closure in Haskell

Context: I need to write a mostly stateless compiler which transforms VM bytecode into machine codes. Most VM commands can be translated statelessly with pure function like the following: compilePop = ["mov ax, @sp", "dec ax", "mov @sp,…
kirilloid
  • 14,011
  • 6
  • 38
  • 52
6
votes
2 answers

Haskell State monad in tracking movement in 2 dimensions

I am trying to follow the movement of an object, on a 2D plane, which has been given a list of commands "forward, left or right". So far I have functions that take in the components of a state of the object (direction, position and moves) and return…
matt
  • 1,817
  • 14
  • 35
6
votes
3 answers

Haskell State Monad

Does the put function of the State Monad update the actual state or does it just return a new state with the new value? My question is, can the State Monad be used like a "global variable" in an imperative setting? And does put modify the "global…
user3169543
  • 1,499
  • 1
  • 10
  • 16
6
votes
4 answers

Pure functional Random number generator - State monad

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 = { …
Dev Maha
  • 1,133
  • 1
  • 11
  • 24
6
votes
3 answers

Use two monads without a transformer

In order to understand how to use monad transformers, I wrote the following code without one. It reads standard input line by line and displays each line reversed until an empty line is encountered. It also counts the lines using State and in the…
ByteEater
  • 885
  • 4
  • 13
6
votes
2 answers

Why can I call a monadic function without supplying a monad?

I thought I had a good handle on Haskell Monads until I realized this very simple piece of code made no sense to me (this is from the haskell wiki about the State monad): playGame :: String -> State GameState GameValue playGame [] = do (_,…
parker.sikand
  • 1,371
  • 2
  • 15
  • 32
6
votes
0 answers

Haskell Data Type With References

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…
Craig
  • 255
  • 1
  • 6
6
votes
1 answer

Creating monads analoguous to the IO Monad with chained state

Hello folks, I'm pretty new to Haskell again this year (after using it in the early 1990s and then again in the early 00's). I'm trying to write some code that uses a pattern that is almost directly analoguous to the example IO monad shown on the…
5
votes
2 answers

Trying to understand scalaz state monad

I'm trying to start using scalaz in my lift project. For that purpose I'm rewriting some code to meet the style. Consider a code for logging in a user: def login: CssSel = { var password = "" def submit() { if (doLogin)…
George
  • 8,368
  • 12
  • 65
  • 106
5
votes
1 answer

How does the higher-order encoding of indexed monads work?

The usual way to define an indexed monad a la Atkey is: class IxMonad m where ireturn :: a -> m i i a ibind :: m i j a -> (a -> m j k b) -> m i k b Another approach is found in the work of McBride (also discussed by him here): type f :-> g =…
SEC
  • 799
  • 4
  • 16
5
votes
0 answers

Does it make sense to use Monad when your code is not ugly without it?

I have a recursive function that receives a data object with number of fields, something like: data MyState = { first :: Int, second :: String, third :: Bool, ... } type Result = Int This recursive function changes it during the…
Sergii Sopin
  • 411
  • 2
  • 11