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
1
vote
0 answers

QuickCheck and State monad

I have written a Haskell module that contains functions that operate on some state. Let's say that the code looks like this (the actual functions may return an actual result instead of (), but this is irrelevant to the question): data StateContext =…
Boulougou
  • 46
  • 1
  • 4
1
vote
2 answers

The reverse state monad in Coq

In Haskell, the following definition of bind is accepted: type RState s a = s -> (a, s) bind :: RState s a -> (a -> RState s b) -> RState s b bind sf f = \s -> let (a, s'') = sf s' (b, s') = f a s in (b, s'') How can I get a similar…
Bob
  • 1,713
  • 10
  • 23
1
vote
3 answers

Monad transformers: Implementation of a stack machine with MaybeT (State Stack)

I'm trying to implement a Maybe-State monad transformer and use it to implement a simple stack machine. The definitions of state monad and maybe should be correct. Now I'm trying to implement pop: pop :: MaybeT (State Stack) Int So that if the…
Gloomy
  • 1,091
  • 1
  • 9
  • 18
1
vote
3 answers

Understanding Monadic Fibonacci

I am learning haskell and learning monads. I've watched and read various tutorials and coded some simple examples for state monad, however I am not able to understand the following piece of code (taken from Haskell Wiki): import…
McGill
  • 49
  • 5
1
vote
1 answer

What is the best way to combine local and global state

I have this global State type GlobalState a = State Int a but one function needs its own local state and access to the GlobalState type LocalState a = State [String] a But I'm not sure now to combine them. Right now I just add local state to the…
ais
  • 2,514
  • 2
  • 17
  • 24
1
vote
1 answer

How to kickstart monad transformer stack from main?

this sort of a follow-up question to my previous one: State and IO Monads My goal is to create a simple text-editor for files. I already have an Editor component that nicely encapsulates all editing actions on an underlying data structure. Thanks to…
DeX3
  • 5,200
  • 6
  • 44
  • 68
1
vote
2 answers

Ideas for custom `do`/`let` syntax (without macros)?

I just built a state monad "object", and would like to be able to bind steps to variables, and link up successive computations, as if with bind/>>=, like how haskell works with monads, and like how clojure's various monad libs work. Here's my…
Josh.F
  • 3,666
  • 2
  • 27
  • 37
1
vote
2 answers

State Monad Bind

I think I understand how the State Monad works. I've managed to write some code which uses the State Monad. I understand how the Monad instance of State works: instance Monad (State s) where return x = State $ \s -> (x,s) (State h) >>= f = State…
1
vote
1 answer

labeling trees in haskell

I have an arbitrary tree and want to transform it into a tree of integers, the original values should be replaced by integers. The same value has to be replaced by the same number at every occurrence. function for traversing tree is provided, and…
N....F
  • 49
  • 7
1
vote
1 answer

How to properly use the StateT monad here?

I'm have to make a very small game in Haskell for a course I follow. So I created the play-function, but now I have trouble changing it's type signature. play :: [[String]] -> StateT GameState IO Score play input = do liftIO $ clearScreen …
user4424299
1
vote
1 answer

How to apply a list of modifciations on a scalaz State Monad without for comprehension

I am currently playing arround with the scalaz State Monad. I want to implement a processing pipeline where a dynamic number of modifications will be applied to an initial State. I want to get out a final State after all processings have been done.…
Jay
  • 1,035
  • 2
  • 11
  • 22
1
vote
2 answers

Extract a random element from a list (and remove it), learning monads

Im learning monads in Haskell with the haskell wikibook and my own newbie experiments As my first step, i've copypasted the parts of the book related to monads to my own PDF using the great HaTeX library (by Daniel Díaz) while lightly reading such…
RamiroPastor
  • 1,085
  • 1
  • 7
  • 18
1
vote
2 answers

State Monad then (>>)

I would like to know the definition of >> for the Haskell state monad. By my guess it pass one state to another: (>>) :: State s a -> State s b -> State s b State a >> State b = State $ \x -> b $ snd ( a x ) or State a >> State b = State $ b . snd…
nobody
  • 414
  • 2
  • 8
  • 18
1
vote
1 answer

Run this monadic computation with notion of state and randomness

I describe the following computation: import Control.Monad.State import Control.Monad.Identity import Control.Monad.Random.Class -- * fair coin fair :: MonadRandom m => m Bool fair = (\p -> p <= 0.5) <$> getRandomR (0,1 :: Double) -- * how do i…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
1
vote
1 answer

read/write another process memory monad in F#

I work on cheat for a single player game. I like function composition, immutability and a code without boilerplate so that's why I decided to write the cheat in F#. I finished something which works fine. I know that the code is far from perfect, but…