monad-transformers

Lift instance of class with a `MonadIO` type-variable to the transformed monad

ⅰ亾dé卋堺 提交于 2021-02-11 06:37:12
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

Lift instance of class with a `MonadIO` type-variable to the transformed monad

雨燕双飞 提交于 2021-02-11 06:36:49
问题 As part of a program where I need to log data from monadic computations, I am trying to define a class to make this more convenient. module Serial where import Data.Int import Data.IORef import System.IO import Control.Monad.Trans import Foreign.Ptr import Foreign.Marshal import Foreign.Storable class MonadIO m => Serial m a where get :: Handle -> m a put :: Handle -> a -> m () One of the things I'd like to be able to do is to define get and put in a 'higher' monad, since some data is

Is there a valid array monad transformer?

点点圈 提交于 2021-01-27 18:33:10
问题 I know how to implement the single linked list monad transformer but couldn't get its array counterpart running. The problem is that there is a grouping effect which renders the transformer only valid for commutative base monads. Here is an example where for the sake of simplicity both the transformer and the base monad are arrays and there is no transformer type wrapper: // ARRAY const arrMap = f => xs => xs.map((x, i) => f(x, i)); const arrAp = tf => xs => arrFold(acc => f => arrAppend(acc)

Use list monad inside monad transformer type classes?

天大地大妈咪最大 提交于 2021-01-27 05:02:15
问题 My goal is to create a function that uses the list monad inside either a ReaderT WriterT stack, or a RWS stack. More generally, how do I use the list monad inside mtl typeclasses such as MonadReader, MonadWriter? Why am I trying to do this? This problem is an exercise in Beginning Haskell. It asks me to "use functionality from both MonadReader and MonadWriter wrapping the base list monad. To check that the function is general, use two different monads to [test] the requested functionality:

Replace scalaz ListT with semantically equivalent cats functionality

柔情痞子 提交于 2020-03-14 18:37:06
问题 cats does not provide ListT monad transformer so how could we rewrite the following snippet which uses scalaz ListT in a for-comprehension to a semantically equivalent snippet in cats import scalaz._ import ListT._ import scalaz.std.option._ val seeds: Option[List[String]] = Some(List("apple", "orange", "tomato")) def grow(seed: String): Option[List[String]] = Some(List(seed.toUpperCase)) def family(seed: String, plant: String): Option[List[(String, String)]] = Some(List(seed -> plant)) (for

FS2 Stream with StateT[IO, _, _], periodically dumping state

a 夏天 提交于 2020-03-02 10:52:23
问题 I have a program which consumes an infinite stream of data. Along the way I'd like to record some metrics, which form a monoid since they're just simple sums and averages. Periodically, I want to write out these metrics somewhere, clear them, and return to accumulating them. I have essentially: object Foo { type MetricsIO[A] = StateT[IO, MetricData, A] def recordMetric(m: MetricData): MetricsIO[Unit] = { StateT.modify(_.combine(m)) } def sendMetrics: MetricsIO[Unit] = { StateT.modifyF { s =>

Scotty monad transformer for per-handler Reader

ⅰ亾dé卋堺 提交于 2020-02-21 12:44:07
问题 In the question Web, Scotty: connection pool as monad reader it is shown how to use ScottyT to embed a Reader monad in the stack to access a static configuration (in that case, a connection pool). I have a similar question, but simpler – or at least I thought so… I want to add a Reader to a single handler (i.e. a ActionT ), not the whole app. I started modifying the program from the question above, but I cannot figure out how to turn an ActionT Text (ReaderT String IO) into the ActionT Text

Carry STRef implicitly in an environment during computation

这一生的挚爱 提交于 2020-01-24 09:20:06
问题 I am working on some bigger computation that requires use of mutable data in some critical moments. I want to avoid IO as much as I can. My model used to constist of ExceptT over ReaderT over State datatype, and now I want to replace State with mentioned ST . To simplify, let's assume I would like to keep single STRef with an Int during the whole computation, and let's skip the ExceptT outer layer. My initial idea was to put STRef s Int into ReaderT 's environment: {-#LANGUAGE Rank2Types#-} {

Type Variable Location in Transformers

老子叫甜甜 提交于 2020-01-23 17:02:23
问题 Consider the State type - or at least a simplified version: newtype State s a = State { runState :: s -> (a, s) } Now, let's say we want to derive the StateT monad transformer. transformers defines it as follows: newtype StateT s m a = StateT { runStateT :: s -> m (a, s) } Here, the m has been placed on the right of the function arrow, but outside the tuple. However, if we didn't know the correct answer, we might instead put m somewhere else: newtype StateT s m a = StateT { runStateT :: m (s

Mixing Threepenny-Gui and StateT

无人久伴 提交于 2020-01-13 19:29:31
问题 I have a question on the interaction of Threepenny-Gui with StateT. Consider this toy program that, every time the button is clicked, adds a "Hi" item in the list: import Control.Monad import Control.Monad.State import qualified Graphics.UI.Threepenny as UI import Graphics.UI.Threepenny.Core hiding (get) main :: IO () main = startGUI defaultConfig setup setup :: Window -> UI () setup w = void $ do return w # set title "Ciao" buttonAndList <- mkButtonAndList getBody w #+ map element