Questions tagged [st-monad]

The strict state-transformer monad. A computation of type `ST s a` transforms an internal state indexed by `s`, and returns a value of type `a`.

The strict state-transformer monad. A computation of type ST s a transforms an internal state indexed by s, and returns a value of type a. The s parameter is either

• an uninstantiated type variable (inside invocations of runST), or

RealWorld (inside invocations of stToIO). It serves to keep the internal states of different invocations of runST separate from each other and from invocations of stToIO.

The >>= and >> operations are strict in the state (though not in values stored in the state). For example,

runST (writeSTRef _|_ v >>= f) = _|_
35 questions
0
votes
0 answers

Hiding phantom type in ST monad

I am writing an (small-step) interpreter for a simple imperative programming language in Haskell. I want to do evaluation outside of the IO monad and I am thus trying to use the ST monad for mutable variables. However I realise that this means…
NatureShade
  • 2,187
  • 1
  • 19
  • 27
0
votes
1 answer

Refactor Haskell monadic code to avoid copy-paste

I have written the following code in Haskell using ST monad and it works. My only question is how do I avoid the copy-paste shown in the code below. When I tried to refactor the code, I got compiler errors I could not fully understand. Is there a…
vamsikal
  • 51
  • 1
  • 6
0
votes
1 answer

Recursive function that returns multable array and a list

I'm trying to build a recursive function, which for simplicity's sake, lets say it takes an list and builds an array and a list. Because I need to both read and write the array as it's being built, I'm using a mutable array so I can do constant time…
Clinton
  • 22,361
  • 15
  • 67
  • 163
0
votes
1 answer

Function argument which transforms in ST Monad

How can I write the following function tt, which has currently type error: t :: Int t = runST $ do ref <- newSTRef 10 readSTRef ref tt :: (STRef s a -> ST s a) -> Int tt f = runST $ do ref <- newSTRef 10 f ref ttTest = tt readSTRef I…
Chul-Woong Yang
  • 1,223
  • 10
  • 17
0
votes
1 answer

Extracting elements from STArray (similar to unzipping)

I'm having a slight problem implementing a particle based fluid simulation in Haskell for a programming competition. I currently have an array of particles that gets modified at each simulation step. Each particle is a tuple of 2 vectors: position…
1 2
3