st-monad

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

一曲冷凌霜 提交于 2020-01-04 06:15:05
问题 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 messages A type of message it can send determined by a function that accepts the current node state (with the possibility of failure) A list of edges Node { nodeState :: NodeState, inbox :: Queue NodeMessage, nodeMessage :: (NodeState -> Maybe NodeMessage),

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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-04 06:12:50
问题 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 messages A type of message it can send determined by a function that accepts the current node state (with the possibility of failure) A list of edges Node { nodeState :: NodeState, inbox :: Queue NodeMessage, nodeMessage :: (NodeState -> Maybe NodeMessage),

Haskell — dual personality IO / ST monad?

微笑、不失礼 提交于 2020-01-02 00:12:09
问题 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 has gotten longer, I do want to put debugging print statements in. Is there any class that provides a dual-personality monad [or typeclass machinery], one which can be a ST or an IO (depending on its type or a "isDebug" flag)? I recall SPJ

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

杀马特。学长 韩版系。学妹 提交于 2020-01-01 08:45:59
问题 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 -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read

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

拈花ヽ惹草 提交于 2020-01-01 08:45:27
问题 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 -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read

Combine ST and List monads in Haskell

人走茶凉 提交于 2019-12-23 15:08:33
问题 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 would like to be able to instantiate them at will, depending on the results of earlier computations. However, the linked documentation for STT mentions explicitly: This monad transformer should not be used with monads that can contain multiple

Haskell — dual personality IO / ST monad?

丶灬走出姿态 提交于 2019-12-04 22:42:31
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 has gotten longer, I do want to put debugging print statements in. Is there any class that provides a dual-personality monad [or typeclass machinery], one which can be a ST or an IO (depending on its type or a "isDebug" flag)? I recall SPJ introduced a "Mutation" class in his "Fun with Type Functions" paper, which used associative types to relate IO

Modeling the ST monad in Agda

情到浓时终转凉″ 提交于 2019-12-04 10:32:01
问题 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 Control.Monad.Trans.State import GHC.Prim (Any) import Unsafe.Coerce (unsafeCoerce) import Data.List newtype ST s a = ST (State ([Any], Int) a) deriving (Functor, Applicative, Monad) newtype STRef s a = STRef Int deriving Show newSTRef :: a -> ST s (STRef s a)

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

纵饮孤独 提交于 2019-12-04 02:12:08
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 -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read mvec i write mvec i (oldval + 1) freeze mvec But what is going on here? What is a PrimMonad ? And is

How to put mutable Vector into State Monad

倾然丶 夕夏残阳落幕 提交于 2019-12-03 13:00:03
问题 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 Show main :: IO () main = do print $ runTraverse (Node Null 5 Null) type MyMon a = StateT (Vector Int) Identity a runTraverse :: Tree Int -> ((),Vector Int) runTraverse t = runIdentity (runStateT (traverse t) (Data.Vector.replicate 7 0)) traverse :: Tree