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

How to implement actions in ST-monad with my own underlying representation (similarly to STRef or STArray) using simple techniques?

I want to manipulate structs of a certain type from FFI through an interface like that provided with STArray or STRef in an ST monad. I'll have my own specific methods with understandable names for the kind of manipulations that are useful for this…
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
10
votes
1 answer

Continuation monad "interface"

The state monad "interface" class MonadState s m where get :: m s put :: s -> m () (+ return and bind) allows to construct any possible computation with State monad without using State constructor. For example, State $ \s -> (s+1, s-1) can…
sdcvvc
  • 25,343
  • 4
  • 66
  • 102
10
votes
0 answers

State monad and strategy pattern

I am redesigning a library and I am not happy with the current design pattern. This question concerns the use of the strategy pattern in conjunction with a State monad I have a Filter. All it does, in its basic implementation, is to take a some…
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
9
votes
2 answers

State and IO Monads

I've been trying to wrap my head around the concept of monads and I've been experimenting with the following example: I have an Editor data-type that represents the state of a text document and some functions that work on it. data Editor = Editor { …
DeX3
  • 5,200
  • 6
  • 44
  • 68
9
votes
2 answers

Breadth-First Search using State monad in Haskell

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…
MazaYong
  • 197
  • 1
  • 8
9
votes
1 answer

Generalized Newtype Deriving

Haskell can derive the instance for MonadState s in T1 below but not in T2 which is however a very similar type. In which way should I modify the code for T2 so that the instance for MonadState s can be automatically derived? {-# LANGUAGE…
Bob
  • 1,713
  • 10
  • 23
9
votes
1 answer

The reverse state monad in OCaml

How would you implement the reverse state monad in OCaml? (Since it relies heavily on laziness, I guess one has to use the Lazy module from the standard library).
Bob
  • 1,713
  • 10
  • 23
9
votes
1 answer

Is this generalization of runST safe?

Control.Monad.ST in the base package contains runST to run the strict state-transformer monad: runST :: (forall s. ST s a) -> a However, I need a generalized version of runST: runSTCont :: (forall s . (forall b . ST s b -> b) -> a) -> a runSTCont f…
user1972140
9
votes
1 answer

How to put mutable Vector into State Monad

I wrote small program in haskell to count all ocurences of Int values in Tree using State Monad with Vector: import Data.Vector import Control.Monad.State import Control.Monad.Identity data Tree a = Null | Node (Tree a) a (Tree a) deriving…
9
votes
1 answer

Why don't you need to use 'lift' when interacting with a nested StateT monadT in this case?

Let's say I have a monadT: type Wrap a = ReaderT Env ( StateT Int ( StateT Int Identity ) ) a The important thing to note here is that one StateT is wrapping another, and both are wrapped inside a third MonadT, namely ReaderT. and the corresponding…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
9
votes
3 answers

Is it better to use the State monad, or to pass state recursively?

I'm just learning Haskell, and trying to figure out the most idiomatic way to implement a line of sight algorithm. The demo code I found uses the state monad, but it seem simpler to me (I'm just a beginner) to pass state recursively. What am I…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
8
votes
2 answers

Nested States in Haskell

I am trying to define a family of state machines with somewhat different kinds of states. In particular, the more "complex" state machines have states which are formed by combining the states of simpler state machines. (This is similar to an object…
8
votes
2 answers

Conditional state monad expressions

I'm using the State monad from the Scala Cats library to compose imperative sequences of state transitions in a functional manner. My actual use-case is quite complicated, so to simplify matters, consider the following minimal problem: there is a…
Mike Allen
  • 8,139
  • 2
  • 24
  • 46
8
votes
2 answers

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

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…
SEC
  • 799
  • 4
  • 16
8
votes
2 answers

State Monad containing Random and List in Haskell

As I'm learning for my exam Functional Programming, I'm still trying to really understand Monads. What better way than to define one yourself? I defined this: newtype ST a = ST (State -> ([a], State)) type State = StdGen Basically a List Monad and…
Rich_Rich
  • 427
  • 3
  • 15