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
18
votes
2 answers

Haskell -- dual personality IO / ST monad?

I have some code that currently uses a ST monad for evaluation. I like not putting IO everywhere because the runST method produces a pure result, and indicates that such result is safe to call (versus unsafePerformIO). However, as some of my code…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
13
votes
2 answers

Is there a proof that runST is indeed pure?

The ST monad, originally devised by Launchbury and Peyton Jones, allows Haskell programmers to write imperative code (with mutable variables, arrays, etc.) while obtaining a pure interface to that code. More concretely, the polymorphic type of the…
Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
11
votes
1 answer

Modeling the ST monad in Agda

This recent SO question prompted me to write an unsafe and pure emulation of the ST monad in Haskell, a slightly modified version of which you can see below: {-# LANGUAGE DeriveFunctor, GeneralizedNewtypeDeriving, RankNTypes #-} import…
András Kovács
  • 29,931
  • 3
  • 53
  • 99
11
votes
1 answer

What's going on in this type signature? (Vector.Mutable modifiers in Haskell)

Mutable vectors in Haskell have three element-level mutators: read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m () swap :: PrimMonad m => MVector (PrimState m) a -> Int ->…
Justin L.
  • 13,510
  • 5
  • 48
  • 83
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…
8
votes
1 answer

syntax of ST monad declaration

I recently began looking at core libraries on Hackage, and there's a recurring idiom I don't understand. Here's an example from the ST module: instance Monad (ST s) where {-# INLINE (>>=) #-} (>>) = (*>) (ST m) >>= k = ST (\ s -> …
planarian
  • 2,047
  • 18
  • 18
5
votes
1 answer

Monad transformer for inserts and total lookups on a Map?

I have a computation where I'm inserting values into a Map and then looking them up again. I know that I never use a key before inserting it, but using (!) freely makes me nervous anyway. I'm looking for a way to get a total lookup function that…
user11228628
  • 1,526
  • 1
  • 6
  • 17
5
votes
1 answer

Just how "unsafe" are Data.Vector's unsafeFreeze/unsafeThaw?

The documentation for Data.Vector.unsafeFreeze says: Unsafe[ly] convert a mutable vector to an immutable one without copying. The mutable vector may not be used after this operation. I would like to characterize exactly what "unsafe" means here.…
cemerick
  • 5,916
  • 5
  • 30
  • 51
5
votes
2 answers

Combine ST and List monads in Haskell

Using the StateT monad transformer, I can create the type StateT s [] a, which is isomorphic to s -> [(a, s)]. Now I would prefer to use the STT monad transformer instead, as I would like to have multiple mutable variables of different types, and…
dremodaris
  • 358
  • 1
  • 9
4
votes
2 answers

Requires MonadPlus (ST a) Instance

I'm reading the paper Typed Logical Variables in Haskell, but I'm failing to understand the details of the ultimate implementation. In particular, the backtracking state transformer introduced in section 4. For some reason, unknown to me, GHC…
emi
  • 5,380
  • 1
  • 27
  • 45
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
0 answers

Coroutine with StateT and ST and IO

having some trouble with a group of monads I'm trying to combine. I'm using monad-coroutine, State and lens (as I have deeply nested state). I had an initial approach where there was a working solution. The main point here is that I can request to…
ksaveljev
  • 550
  • 2
  • 16
4
votes
0 answers

Is it safe to unsafeThaw an indexed unboxed vector?

I just posted this code: import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Algorithms.Intro as VAlgo argSort :: (Ord a, VU.Unbox a) => VU.Vector a -> VU.Vector Int argSort xs = VU.map fst $ VU.create $ do xsi <-…
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319
3
votes
1 answer

Haskell ST Monad: No instance for (MArray (STArray s) Int (ST s1))

I have been learning Haskell for the past month or two, and recently solved this coding problem. The additional challenge was to do the task without extra space and in linear time, which I did not think would be possible to do in a purely functional…
Zafer Cesur
  • 497
  • 2
  • 10
3
votes
3 answers

How can I implement a Fisher-Yates shuffle in Scala without side effects?

I want to implement the Fisher-Yates algorithm (an in-place array shuffle) without side effects by using an STArray for the local mutation effects, and a functional random number generator type RNG[A] = State[Seed,A] to produce the random integers…
arya
  • 946
  • 5
  • 14
1
2 3