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

Mixing IO w/ ST Monad - "type variable `s2' would escape its scope"

I decided to simplify my code to see what conditions produced the error. I start with a simple nested "s" similar to ST s (STArray s x y) like so: {-# LANGUAGE RankNTypes #-} import Control.Monad.ST import Control.Applicative data Foo s = Foo {…
TheCriticalImperitive
  • 1,457
  • 1
  • 10
  • 23
2
votes
1 answer

Not being able spot the problem in the code in Dijkstra’s Haskell implementation

I understand that asking “why my code does not work” is not the best question. However, I am asking as I wish to learn more about using monads in Haskell in an algorithmic context for graph theory problems, and took the following code as a starting…
Danish A. Alvi
  • 161
  • 1
  • 8
2
votes
0 answers

How to migrate Control.ST code to Idris 2 (Control.App)?

Idris 2 doesn't have Control.ST, only Control.Monad.ST which is a completely different beast (it is basically the same as Haskell's Control.Monad.ST, i.e. mutable references behind a safe, pure interface). It seems that Control.App is…
Cactus
  • 27,075
  • 9
  • 69
  • 149
2
votes
0 answers

Looping inside the ST monad

I'm trying to implement insertion sort in Haskell on Data.Array.ST, with a monadic comparison function. i.e., sortByM :: (Monad m, Ix i, Enum i, Eq i) => (e -> e -> Ordering) -> STArray s i e -> m (ST s ()). Here's my code, for context: import…
Evelyz
  • 71
  • 1
  • 4
2
votes
1 answer

Specification of `State#`

However, the documentation for STT says: This monad transformer should not be used with monads that can contain multiple answers, like the list monad. The reason is that the state token will be duplicated across the different answers and this…
dremodaris
  • 358
  • 1
  • 9
2
votes
1 answer

How to hoist Conduit of STT

I've been trying to write an implementation of the function: foo :: Monad m => ConduitM i o (forall s. STT s m) r -> ConduitM i o m r But I've been failing at every turn with the error: Couldn't match type because variable `s` would escape its…
John Ky
  • 31
  • 3
2
votes
1 answer

How to improve this algorithm by 1)use arrays, 2)avoid list concatenation (lazy lists?)?

I tried to learn how the STArray works, but I couldn't. (Doc is poor, or at least the one I found). Any way, I have the next algorithm, but it uses a lot of !!, which is slow. How can I convert it to use the STArray monad? -- The Algorithm prints…
Lay González
  • 2,901
  • 21
  • 41
2
votes
1 answer

Using Monad/ST for non-concurrent message passing in a mutable graph

I am trying to work out a data structure for the following situation. Graph Structure I plan to have a graph of nodes with un-weighted, directed edges: Graph = [Node] Each node has: Some TBD internal (persistent) state A queue of incoming…
TheCriticalImperitive
  • 1,457
  • 1
  • 10
  • 23
2
votes
1 answer

How do I make a new data type, based on a vector, within the ST monad

Closely related to my most recent question on handling large data blocks, I have reached the point in which I need to take a large immutable data block, make it mutable for some operations, and then make it immutable again when I am done. Since I…
Savanni D'Gerinel
  • 2,379
  • 17
  • 27
2
votes
1 answer

using and returning multiple STUArrays

I've been working out how to create and use multiple STUArrays in an ST computation. The specific scenarios are: create multiple arrays but return only one of them create multiple arrays but return none of them create multiple arrays and return…
ErikR
  • 51,541
  • 9
  • 73
  • 124
1
vote
0 answers

Moving an ST computation to a sub-computation

I have the following code running in ST using random numbers: module Main import Data.Vect import Data.Fin import Control.ST import Control.ST.Random %default total choose : (Monad m) => (rnd : Var) -> (n : Nat) -> Vect (n + k) a -> ST m (Vect…
Cactus
  • 27,075
  • 9
  • 69
  • 149
1
vote
0 answers

Haskell: handling cyclic dependencies while tying the knot

While writing a programming language that will feature local type inference (i.e. it will be capable of inferring types with the exception of function parameters, like Scala), I've run into a problem with cyclic dependencies. I perform…
0
votes
1 answer

Should the state of the ST not change in order to be executed by the runST?

type ST s a = ST (s -> (s, a)) runST :: (forall s. ST s a) -> a runST (ST f) = case (f realWorld) of (_, a) -> a If you look closely, there are many errors, but the overall structure of runST is as above. So, if you want to apply runST to a value…
kwonryul
  • 481
  • 3
  • 10
0
votes
0 answers

Does Haskell "hackage" overlaps two different type signature with same name?

While I was exploring the library, I saw the following code. https://hackage.haskell.org/package/text-2.0.2/docs/src/Data.Text.Internal.Lazy.Fusion.html#unstreamChunks look at unstreamChunks. This code bundles the Stream of Char into chunks and…
kwonryul
  • 481
  • 3
  • 10
0
votes
1 answer

Modify ST dependent environment in ReaderT – problem with `local` function

This question is a sequel of this thread: https://stackoverflow.com/a/54317095/4400060 I was asking there about carrying STRef in ReaderT's environment and performing ST-actions under it. My setup now looks like: import Data.HashTable.ST.Cuckoo as…
radrow
  • 6,419
  • 4
  • 26
  • 53