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
4
votes
1 answer

Why is newtype commonly used instead of type with the state monad

Almost all of examples I've seen of the State Monad have been wrapped inside a newtype. {-# LANGUAGE GeneralizedNewtypeDeriving #-} import Control.Monad.State import Control.Applicative data Bazzar = Bazzar { valueOne :: Int ,…
akst
  • 935
  • 11
  • 21
4
votes
2 answers

What is this simple function called?

Have you already seen the following function? What is it called? What is it useful for? can it be defined more generically than just for StateT? simpleFunction (StateT f) = StateT $ (\s -> return (f s, s)) By the way, ghc gives it the type Monad n…
4
votes
2 answers

How to nondeterministically put a value in a state?

In the following code, how can I replace put 1 with some code that insert nondeterministically 1 or 2 in the state? import Control.Monad.List import Control.Monad.Trans.State test :: StateT Int [] Int test = do put 1 v <- get return v
Bob
  • 1,713
  • 10
  • 23
4
votes
1 answer

How do I use the Supply monad to create a function that generates globally unique names?

Background: I'm doing a code translation project that requires me to generate variable names. None of the names I generate should be duplicates of each other. I'm really frustrated since this would be stupidly simple and elegant with a Python…
machine yearning
  • 9,889
  • 5
  • 38
  • 51
4
votes
2 answers

How should I combine St monad and State monad (or equivalent)?

I am building code to gain understanding, in fact a Solitaire solver. I have a simple brute force implementation which uses the State monad, really just to prove I can use it (it only keeps a count of each move evaluated). But now I want to use…
hdb3
  • 239
  • 1
  • 10
4
votes
1 answer

State Monad - While-loops

This question has been inspired by this question. I understand the example (ListBuilder) but I have not been able to create a while loop for my state monad. What is not clear to me is how to bind the body of the whileloop as the iterations follow…
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
4
votes
1 answer

Justification of using >>

In Real World Haskell chapter, they give justification for (>>) like this: We use this function when we want to perform actions in a certain order, but don't care what the result of one is. They then give an nice example demonstrating it: ghci >…
Sibi
  • 47,472
  • 16
  • 95
  • 163
4
votes
1 answer

Haskell: attempting to desugar simple State monad get and put

To study the details of the State monad, I'm trying to create myself a complete desugared version of a simple state monad function, completing the thought started in How does 'get' actually /get/ the initial state in Haskell?, in the answer by J…
gwideman
  • 2,705
  • 1
  • 24
  • 43
4
votes
3 answers

Could someone walk through how bind in the state monad is implemented ( among other things )?

Despite reading through the really clear explanation in LYAH, and then Haskell Wiki, and some other stuff, I am still confused about how the state monad is implemented. I think I understand what it is though I'm not confident. So let's say I have…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
4
votes
1 answer

How can I build a nondeterministic state monad in Haskell?

I want to build a nondeterministic state monad in Haskell. This will allow me to generate all the elements in my search space using the built up state to prune bad locations. Suppose I have the following (pseudo-)code: primitives :: [State Int…
Eyal
  • 1,094
  • 10
  • 16
4
votes
3 answers

haskell - state monad function get?

one question according to the get function of the State Monad: If I run runState get 1 I got the result (1,1) and this is ok for me because the get function set the result value to the state and in this case the state is 1. Thus, (1,1) is the…
jimmyt
  • 491
  • 4
  • 10
4
votes
3 answers

How can I initialize state in a hidden way in Haskell (like the PRNG does)?

I went through some tutorials on the State monad and I think I got the idea. For example, as in this nice tutorial: import Data.Word type LCGState = Word32 lcg :: LCGState -> (Integer, LCGState) lcg s0 = (output, s1) where s1 = 1103515245 * s0…
Jay
  • 9,585
  • 6
  • 49
  • 72
3
votes
2 answers

Complex State Monad Structure

I am still a newbie to Haskell and I think I am over my head right now. I have code that looks like the following. data World = World { intStack :: [Int], boolStack :: [Bool] } deriving Show instance IntStack World where getIntStack =…
Charles Durham
  • 1,707
  • 11
  • 17
3
votes
1 answer

runState inside a State Monadic function not working

I am trying to solve the problem 2.8 of "AI - A Modern Approach" book which involves a grid of cells and choosing random moves to navigate the grid. 2.7 Implement an environment for a n X m rectangular room, where each square has a 5% chance of…
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
3
votes
2 answers

Brick not exporting Next or continue

I am attempting to build a CLI interface for a basic app using Haskell and the Brick package. In all examples I have seen (including the documentation), the handleEvent function has the following type signature, in order to tell Brick to continue or…
Alex Scriba
  • 95
  • 1
  • 8