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

Can't find a proper signature for a function using STUArray (neither can GHC)

I built a function for finding the determinant of a matrix using the ST-Monad and unboxed STArrays (STUArray). The type for a matrix is the following: newtype Matrix e = Matrix (Array Int (UArray Int e)) that is, an immutable array containing…
scravy
  • 11,904
  • 14
  • 72
  • 127
4
votes
2 answers

Haskell: iterate in State, how to force the behaviour I want?

This is my first posting on SO, and I'm relatively new to Haskell, so please excuse any missteps or if my code is not idiomatic! Consider the following two intuitive descriptions of: a, f(a), f(f(a))... A. a list containing: a, the application of f…
Bilal Barakat
  • 1,405
  • 2
  • 9
  • 11
4
votes
2 answers

Haskell instance of `bind` for a custom type

I'm trying to create an instance for bind operator (>>=) to the custom type ST a I found this way to do it but I don't like that hardcoded 0. Is there any way to implement it without having the hardcoded 0 and respecting the type of the…
Nibblex
  • 111
  • 1
  • 6
4
votes
1 answer

How to preserve the state of the monad stack in the IO exception handler?

Consider the following program. import Control.Monad.State import Control.Monad.Catch ex1 :: StateT Int IO () ex1 = do modify (+10) liftIO . ioError $ userError "something went wrong" ex2 :: StateT Int IO () ex2 = do x <- get …
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
4
votes
0 answers

Evaluator for an AST that describes both pure and (indexed monadic) impure values

I have an AST which describes both pure and impure computations, with the impure computations keeping track of state at the type level. I'm using a HOAS representation; the relevant constructors of the AST GADT are as follows: data AST a where Lam…
4
votes
2 answers

StateMonad instance for TeletypeIO

So, I have this datatype (it's from here: https://wiki.haskell.org/IO_Semantics): data IO a = Done a | PutChar Char (IO a) | GetChar (Char -> IO a) and I thought of writing a StateMonad instance for it. I have already written Monad and…
arryn
  • 329
  • 2
  • 9
4
votes
1 answer

Understanding the State Monad

I am learning about the State monad in the book "Learn You a Haskell for Great Good!" by Miran Lipovaca. For the following monad instance: instance Monad (State s) where return x = State $ \s -> (x,s) (State h) >>= f = State $ \s -> let (a,…
ceno980
  • 2,003
  • 1
  • 19
  • 37
4
votes
1 answer

Why does this type annotation make my functional dependency conflict go away? (and why does it only happen on some versions of GHC?)

So I've been playing around with MonadState class and I have encountered something I consider very strange. I can try to write a monad like the following: test :: ( MonadState Int m , MonadState Bool m ) => m () test = do ((+1) <$> get)…
Wheat Wizard
  • 3,982
  • 14
  • 34
4
votes
1 answer

Chaining a number of transitions with the state Monad

I am starting to use the state monad to clean up my code. I have got it working for my problem where I process a transaction called CDR and modify the state accordingly. It is working perfectly fine for individual transactions, using this function…
Luis Sisamon
  • 101
  • 7
4
votes
1 answer

What does the get and put function do?

From our lecture notes: get' state = (state, state) put' item state = ((), item) -- () is void value data State s a = State (s -> (a, s)) -- Functions get and put: -- (sic!) get :: State s s get = State get' put :: s -> State s () put…
user8314628
  • 1,952
  • 2
  • 22
  • 46
4
votes
1 answer

Recursions with the State-Monad

I am using the State-Monad from the cats library to take care of the state of a card game I am implementing in Scala. I have a function simulateGame which should end as soon as the status of a the current state is Over. The function looks like…
4
votes
1 answer

A monad for piecewise mutable state

I know how ordinary State works (edit: apparently not!). If i need to create an array, and it is inconvenient to create the entire array at once, I can create an STArray, populate it, and then freeze and return a normal immutable array to the…
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
4
votes
2 answers

Where is the memory leak in using StateT s IO a?

Intention: Small application to learn Haskell: Downloads a wikipedia-article, then downloads all articles linked from it, then downloads all articles linked from them, and so on... until a specified recursion depth is reached. The result is saved to…
4
votes
1 answer

Re-dress a ST monad as something similar to the State monad

Here's the scenario: Given is a C library, with some struct at its core and operations thereon provided by an abundance of C functions. Step 1: Using Haskell's FFI a wrapper is created. It has functions like myCLibInit :: IO MyCLibObj, myCLibOp1 ::…
mcmayer
  • 1,931
  • 12
  • 22
4
votes
2 answers

Haskell -- Chaining two states using StateT monad transformers

I have two or more independent states to track in one Haskell application. I am declaring two new type classes using type MonadTuple m = MonadState (Int, Int) m type MonadBool m = MonadState Bool m The monad transformer stack is declared as type…
uucp
  • 379
  • 2
  • 9